如何循环显示数组键type
的值而不循环其他键,因为我有超过500个数组行并减少时间。
$details = array(
array('year' => '1990'),
array('year' => '1995'),
array('condition' => 'working'),
array('type' => 'bus'),
array('type' => 'car'),
array('type' => 'bike'),
);
答案 0 :(得分:4)
您可能正在寻找PHP 5.5中引入的array_column
(但是这仍然会在整个数组内部循环以收集哪些子数组具有此键):
<?php
$details = array_column($details, 'year');
print_r($details);
/*
Array
(
[0] => 1990
[1] => 1995
)
*/
For older versions of PHP the author himself has a polyfill on github
答案 1 :(得分:0)
PHP数组
$details = array(
array('year' => '1990'),
array('year' => '1995'),
array('condition' => 'working'),
array('type' => 'bus'),
array('type' => 'car'),
array('type' => 'bike'),
);
创建仅包含“类型”索引的数组
$names = array_map(function ($v)
{
return $v['type'];
},
$details);
$names = array_filter($names);
print_r($names);
输出
Array ( [3] => bus [4] => car [5] => bike )