Php 在使用稀疏键访问内部数组时,如何遍历二维数组?
我有一个2D数组$UserDesignedCategoryPhp 在使用稀疏键访问内部数组时,如何遍历二维数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个2D数组$UserDesignedCategory Array ( [1] => Array ( [0] => CEO [1] => Assistant Art Director [2] => Assistant Choreographer [3] => Assistant Creative Director [4] => Assistant Direct
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
)
)
我有稀疏的键,因此无法对($I=0;I‘小于’计数;$I++)使用
我已经使用了foreach($userDesignatedCatogery作为$key=>$value),但它也没有帮助
如何遍历此数组以访问内部数组?数组的简单遍历
<?php
foreach($userDesignatedCatogery as $index => $pull) {
echo '#' . $index . '<br>';
foreach($pull as $id => $position)
echo $id . ' => ' . $position . '<br>';
}
您有两种方法:
第一个:
$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
}
}
如何仅访问整个单个内部阵列?我必须找到userDesginationCategory[0]与所有内部数组的交集。$key=key($UserDesignedCategory)$main=$UserDesignedCategory[$key];未设置($UserDesignedCategory[$key])$结果=[];foreach($userDesignedCategory as$index=>$values){如果($intersect=array\u intersect($main,$values))$result[$index]=$intersect;}/$result-有你的交集你的期望结果是什么?如何在第二种方法中访问内部数组的键?@KaranTikku你的数组没有键名,你可以计算索引,像这样:foreach($userdesignatedcography作为$item){$count=0;foreach($item作为$subitem){$current_index=$count;$count++;}}
I使用array_search()访问内部元素键。@KaranTikku这是为了在数组中搜索,要使用这个函数,您最有价值找到键!我的代码通过计算步数来显示我现在的位置!如果你的问题解决了,我很高兴。