我有这个数组
[result] => Array
(
[achievements] => Array
(
[0] => Array
(
[id] => 92
[achievements] => Array
(
[0] => Array
(
[id] => 6
[rewardItems] => Array
(
)
[49] => Array
(
[id] => 1956
[rewardItems] => Array
(
[0] => Array
(
[name] => inv_misc_book_11
我需要所有[rewardItems]数组中的[name](如果存在的话)。
我试过了,但是没有结果,我做错了什么?
foreach ($r['result']['achievements']['0']['achievements']['rewardItems'] as $item) echo '
', $item['name'], '.
echo '
您已经在这个结构中得到了一些嵌套循环来迭代。
<?php
$r = array(
'result' => Array
(
'achievements' => Array
(
0 => Array
(
'id' => 92,
'achievements' => Array
(
0 => Array
(
'id' => 6,
'rewardItems' => Array
(
)
),
49 => Array
(
'id' => 1956,
'rewardItems' => Array
(
0 => Array
(
'name' => 'inv_misc_book_11'
)
)
)
)
)
)
)
);
foreach ($r['result']['achievements']['0']['achievements'] as $achievement){
if(!empty($achievement['rewardItems'])){
foreach ($achievement['rewardItems'] as $item){
echo "$item[name]<br/>".PHP_EOL;
}
}
}