Php数组映射删除空数组
我有一个数组:Php数组映射删除空数组,php,Php,我有一个数组: $array = [ 0 => [], 1 => ['name' => 'test'] 2 => ['name' => 'test 2'] ]; 我使用函数数组_映射获取名称: $names = array_map(function($item) { return $item['name'] ?? null; }, $array); 我在$names中获得“”,如何跳过[]而只获得名称?您应该能够使用array\u fil
$array = [
0 => [],
1 => ['name' => 'test']
2 => ['name' => 'test 2']
];
我使用函数数组_映射获取名称:
$names = array_map(function($item) {
return $item['name'] ?? null;
}, $array);
我在$names中获得“”,如何跳过
[]
而只获得名称?您应该能够使用array\u filter()
通常,快速而肮脏的答案是在
数组映射之后进行一次排序,例如:
$names = array_filter(array_map(function($item) {
return $item['name'] ?? null;
}, $array));
这当然是一个漫长的写作过程:
$names = array_column($array, 'name'));
有关详细信息,请参阅手册