如何通过稀疏键访问内部数组来遍历2D数组?
我有这个二维数组$ userDesignatedCategory。如何通过稀疏键访问内部数组来遍历2D数组?
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)
但它没有帮助的。我如何遍历这个数组访问内部数组?
数组的简单遍历。
<?php
foreach($userDesignatedCatogery as $index => $pull) {
echo '#' . $index . '<br>';
foreach($pull as $id => $position)
echo $id . ' => ' . $position . '<br>';
}
我怎样才能访问整个单一的内部阵列?我必须找到userDesginationCategory [0]与所有内部数组的交集。 –
$ key = key($ userDesignatedCategory); $ main = $ userDesignatedCategory [$ key]; unset($ userDesignatedCategory [$ key]); $ result = []; foreach($ userDesignatedCategory as $ index => $ values)if($ intersect = array_intersect($ main,$ values)) $ result [$ index] = $ intersect; } // $结果 - 有你的交集 – dekameron
你有两种方式:
第一招:
$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
}
}
如何在第二种方法中访问内部数组的键? –
@KaranTikku你的数组没有键名,你可以计算索引,像这样:'foreach($ userDesignatedCategory as $ item){$ count = 0; foreach($ item as $ subitem){$ current_index = $ count; $ count ++; }}' – aidinMC
我使用array_search()来访问内部元素键。 –
究竟什么是你预期的结果? – mickmackusa