如何在多维数组中按键[sub1]的值进行搜索,并显示那里的展示次数和收入。数组行可以包含无限的国家/地区。
Array
(
[rows] => Array
(
[0] => Array
(
[sub1] => Spain
[impressions] => 1022
[earnings] => 0.13
)
[1] => Array
(
[sub1] => Germany
[impressions] => 801
[earnings] => 0.01
)
[2] => Array
(
[sub1] => Italy
[impressions] => 684
[earnings] => 0.35
)
)
)
答案 0 :(得分:1)
如果sub1
是唯一的,那么只需重新索引,然后使用该国家就可以轻松访问:
$lookup = array_column($array['rows'], null, 'sub1');
echo $lookup['Italy']['impressions'];
也许首先测试一个有效的国家:
if(isset($lookup['Italy'])) {
echo $lookup['Italy']['impressions'];
}