我有一个数组,它由不确定数量的数组组成,递归地(n 级深)。每个数组可能包含一个name
键。我想创建这些值的唯一列表。
例子
假设数组是:
$bigArray = array(
'name'=>'one',
'something'=>array(
'name'=>'two',
'subthing'=>array('name'=>'three')
),
'anotherthing'=>array('name'=>'one')
);
预期的结果是:
$uniques = array('one', 'two', 'three') // All the 'name' keys values and without duplicates.
这是我的尝试。
我的方法是使用array_walk_recursive
传递一个$uniques
数组作为参考,并允许函数更新该值:
$uniques = array();
function singleOut($item, $key, &$uniques) {
if ($key == 'name' && !in_array($itm,$uniques,true) )
$uniques[] = $item;
}
array_walk_recursive($bigArray, 'singleOut', $uniques);
但是,它对我不起作用。