您需要使用second
foreach()
迭代
key
区分记录以及适当添加
$allGridCount = [];
foreach ($allMultigridRes as $gMKey => $GMvalue){
if(!empty($GMvalue)){
foreach ($GMvalue as $gMKey2 => $gMValue2){
if(!empty($gMValue2)){
foreach ($gMValue2 as $gMKey3 => $gMValue3){
$allGridCount[$gMKey2][$gMKey3][$gMValue3] = (isset($allGridCount[$gMKey2][$gMKey3][$gMValue3])) ? $allGridCount[$gMKey2][$gMKey3][$gMValue3]+1 : 1;
}
}
}
}
}
echo "<pre/>";print_r($allGridCount);
输出:-
https://eval.in/1004977
注:-
而不是
!empty()
使用
is_array() && count()
检查它是否是一个数组,并在其中包含一些值以迭代furhter
$allGridCount = [];
foreach ($allMultigridRes as $GMvalue){
if(is_array($GMvalue) && count($GMvalue) >0){
foreach ($GMvalue as $gMKey2 => $gMValue2){
if(is_array($gMValue2) && count($gMValue2) >0){
foreach ($gMValue2 as $gMKey3 => $gMValue3){
$allGridCount[$gMKey2][$gMKey3][$gMValue3] = (isset($allGridCount[$gMKey2][$gMKey3][$gMValue3])) ? $allGridCount[$gMKey2][$gMKey3][$gMValue3]+1 : 1;
}
}
}
}
}
echo "<pre/>";print_r($allGridCount);