我有一个只读的多维关联数组,我需要从中提取特定值以生成带有某些值的输出。
数组如下所示:
array (
'Dogs' =>
array (
0 =>
array (
'Name' => 'Big Dogs',
'ID' => '32',
'Brown dogs' =>
array (
0 =>
array (
'Name' => '4 legged dogs',
'Retrievers' =>
array (
0 =>
array (
'Name' => 'Fido',
'ID' => '11',
'Owner' => 'Billy',
'IsaBiter' => true,
),
),
),
1 =>
array (
'Name' => '3 legged dogs',
'Retrievers' =>
array (
0 =>
array (
'Name' => 'Spot',
'ID' => '12',
'Owner' => 'Sally',
'IsaBiter' => false,
),
),
),
ETC..
还有一个嵌套的 foreach 循环,它贯穿数组,可以打印数组的所有值,如下所示:
echo "<ul>";
foreach($myArray as $arr1 => $val1) {
foreach($val1 as $arr2 => $val2) {
foreach($val2 as $val3) {
echo "<li>" . $val3 . "<ul>";
foreach($val3 as $arr4 => $val4) {
foreach($val4 as $arr5 => $val5) {
echo "<li>" . $val5;
foreach($val5 as $arr6 => $val6) {
//echo $val6;
foreach($val6 as $arr7 => $val7) {
echo $val7 . "<br />"; //dumps the details
}
echo "</li>";
}
}
}
echo "</ul>";
echo "</li>";
}
}
}
理想情况下,我希望能够 1)排除我不想显示的值(ID、某些数组级别名称等,目前它显示全部)和 2)显示特定的值(即名称、所有者、IsaBiter),以便我可以格式化结果更干净,类似于:
Big Dogs
Brown Dogs
Retrievers
Name: Fido
Owner: Billy
IsaBiter: true
Name: Spot
Owner: Sally
IsaBiter: false