忽略此分组数组中的customer_name重复项的最佳方法是什么?我只想显示一个唯一名称列表。
Array
(
[1] => Array
(
[0] => Array
(
[model_id] => 1
[work_order] => 10999
[id] => 1
[model_name] => STC1.5
[stock] => 3
[free] => 1
[customer_name] => Jane Doe
)
[1] => Array
(
[model_id] => 1
[work_order] => 10998
[id] => 1
[model_name] => STC1.5
[stock] => 3
[free] => 1
[customer_name] => Jane Doe
)
)
)
答案 0 :(得分:0)
假设您仅关心唯一名称,而$arr
是:
Array
(
[1] => Array
(
[0] => Array
(
[model_id] => 1
[work_order] => 10999
[id] => 1
[model_name] => STC1.5
[stock] => 3
[free] => 1
[customer_name] => Jane Doe
)
[1] => Array
(
[model_id] => 1
[work_order] => 10998
[id] => 1
[model_name] => STC1.5
[stock] => 3
[free] => 1
[customer_name] => Jane Doe
)
)
)
然后您可以使用:
$unique_names = array_keys(array_flip(array_column($arr[1],'customer_name')));
如果您担心微优化,请尝试:
$unique_names = array();
foreach( $arr[1] as $v )
{
$unique_names[$v['customer_name']] = $v['customer_name'];
}
答案 1 :(得分:0)
您可以使用名称为array_column的名称创建一个数组,并在其上使用array_unique。
然后使用array_intersect_key从主数组中获取相应的子数组。
$name = array_unique(array_column($arr[1], "customer_name"));
$unique = array_intersect_key($arr, $name);