如何在使用稀疏键访问内部数组时遍历2D数组?

时间:2017-07-30 17:07:17

标签: php arrays multidimensional-array

我有这个2D数组$ userDesignatedCategory。

Array ( 
    [1] => Array (
        [0] => CEO 
        [1] => Assistant Art Director 
        [2] => Assistant Choreographer 
        [3] => Assistant Creative Director 
        [4] => Assistant Director 
        [5] => Assistant Editor 
        [6] => Assistant Equipment Engineer 
        [7] => Assistant Hair Dresser 
        [8] => Assistant Lighting Director 
        [9] => Assistant Make Up Artist 
    ) 
    [2] => Array ( 
        [0] => Senior Developer 
    ) 
    [3] => Array ( 
        [0] => CEO 
        [1] => Script Supervisor 
        [2] => Creative Director 
        [3] => Anchor 
        [4] => Executive Producer 
        [5] => Director 
        [6] => Actor 
    ) 
    [7] => Array ( 
        [0] => Director 
        [1] => Executive Producer 
        [2] => Journalist 
        [3] => Producer 
    ) 
    [10] => Array ( 
        [0] => Head of Division 
        [1] => Vice President 
    ) 
    [11] => Array ( 
        [0] => Anchor 
        [1] => Chairman 
        [2] => Co Founder 
        [3] => Creative Director 
        [4] => Director 
    ) 
    [13] => Array ( 
        [0] => Associate Producer 
    ) 
    [16] => Array ( 
        [0] => Accounts Manager 
    ) 
    [20] => Array ( 
        [0] => Adventure Cameraperson 
        [1] => Cameraperson 
        [2] => Director Of Photography 
        [3] => Underwater Cameraperson 
    ) 
    [21] => Array ( 
        [0] => Director 
        [1] => Screenplay Writer 
        [2] => Writer 
    ) 
    [28] => Array ( 
        [0] => Director 
    ) 
    [50] => Array ( 
        [0] => Cameraperson 
    ) 
    [73] => Array ( 
        [0] => Accounts Manager 
        [1] => Actor 
        [2] => Aerial Cameraperson 
        [3] => Anchor 
    ) 
    [78] => Array ( 
        [0] => Accounts Manager 
        [1] => Aerial Cameraperson 
        [2] => Animator 
    ) 
    [79] => Array ( 
        [0] => Actor 
        [1] => Anchor 
        [2] => Adventure Cameraperson 
        [3] => Aerial Cameraperson 
        [4] => Animation Director 
        [5] => Animator 
        [6] => Assistant Make Up Artist 
        [7] => Assistant Manager 
    ) 
    [82] => Array ( 
        [0] => Adventure Cameraperson 
        [1] => Cameraperson 
        [2] => Director Of Photography 
        [3] => Associate Producer 
    )
    [86] => Array ( 
        [0] => Director 
        [1] => Producer 
        [2] => Writer 
    ) 
    [87] => Array ( 
        [0] => Co Founder 
        [1] => Vice President 
    ) 
) 

我有稀疏键,所以我不能使用for($i=0;i 'less than' count;$i++)

我使用了foreach($userDesignatedCatogery as $key => $value),但它也没有帮助。

如何遍历此数组以访问内部数组?

2 个答案:

答案 0 :(得分:3)

数组的简单遍历。

<?php

  foreach($userDesignatedCatogery as $index => $pull) {
    echo '#' . $index . '<br>';
    foreach($pull as $id => $position)
        echo $id . ' => ' . $position . '<br>';
  } 

答案 1 :(得分:0)

你有两种方式:

第一个:

$count = count($userDesignatedCategory);
for ($i=0; $i < $count; $i++) { 
    foreach ($userDesignatedCategory[$i] as $item) {
        //some codes here
    }
}

第二个:

foreach ($userDesignatedCategory as $item) {
    foreach ($item as $subitem) {
        // some code here
    }
}