我在php中有以下数组:
Array
(
[0] => Array
(
[city] => London
[institute_count_per_city] => 10
)
[1] => Array
(
[city] => Leeds
[institute_count_per_city] => 10
)
[2] => Array
(
[city] => Edinburgh
[institute_count_per_city] => 10
)
[3] => Array
(
[city] => GrandTotal
[institute_count_per_city] => 30
)
)
现在,最后一个子数组将始终具有常量key => value([city] => GrandTotal)。我想为[city] => GrandTotal获取[institute_count_per_city]的值。
实现这一目标的有效方法是什么。
答案 0 :(得分:0)
没有比迭代所有元素并检查值是否匹配更好的方法了。
$result = null;
foreach($arr as $key => $item) {
if($item['city'] == 'GrandTotal') {
$result = $item;
break;
}
}
答案 1 :(得分:0)
因为你说它永远是最后的......简单快捷:
$GrandTotal = $array[count($array) -1]['institute_count_per_city'];