PHP - array_map 2列并按键删除重复行
PHP - array_map 2 columns and remove duplicate rows by key
我有以下从外部动态生成的数组 API:
$purchases = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14),
array('product_id' => 7, 'receipt' => R02310),
array('product_id' => 5, 'receipt' => E11403)
);
期望的输出:
$purchases_new = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14)
);
由于 API 的性质,我只能使用 array_map:
提取数据
$purchases_unique = array_unique(
array_map(function($pid){ return $pid['product_id']; }, $purchases)
);
print_r($purchases_unique);
产出
array((array[251] => 7) array([252] => 5))
输出完全不是我想要的:( How to array_map 2 columns product_id'
and receipt
and remove duplicate rows based on product_id
?
我想实现以下目标:
array_map
两列? (product_id
和 receipt
)
- 删除
product_id
基于 product_id
键的重复行?
我认为这会满足您的需求:
只需将唯一的 product_ids(带收据)添加到新数组:
$purchases_new = array_reduce($purchases, function($acc, $p) {
if(!in_array($p['product_id'], array_column($acc, 'product_id'))) {
$acc[] = $p;
}
return $acc;
}, []);
输出:
array (
0 =>
array (
'product_id' => 7,
'receipt' => 'R13D13',
),
1 =>
array (
'product_id' => 5,
'receipt' => 'Y28Z14',
),
)
根据评论 It does not matter if it's the first or last occurrence
,您可以使用 foreach
并为非重复项创建一个 $result
数组。
对于每次迭代,使用product_id
作为数组键并检查它是否已经被设置。
如果不是,则添加。
$purchases = array(
array('product_id' => 7, 'receipt' => "R13D13"),
array('product_id' => 5, 'receipt' => "Y28Z14"),
array('product_id' => 7, 'receipt' => "R02310"),
array('product_id' => 5, 'receipt' => "E11403")
);
$result = [];
foreach($purchases as $key => $purchase) {
if (!isset($result[$purchase["product_id"]])) {
$result[$purchase["product_id"]] = $purchase;
}
}
print_r($result);
结果
Array
(
[7] => Array
(
[product_id] => 7
[receipt] => R13D13
)
[5] => Array
(
[product_id] => 5
[receipt] => Y28Z14
)
)
如果您想要第一次出现,请使用@TheFirstBird 答案。如果你想要最后一次出现,你可以使用 array_column
和 array_values
更简单地购买:
$newArr = array_values(array_column($purchases, null, "product_id"));
注意 array_column
的第二个参数为 null,表示获取整个元素。
实例:3v4l
参考文献:array-column, array-values
我有以下从外部动态生成的数组 API:
$purchases = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14),
array('product_id' => 7, 'receipt' => R02310),
array('product_id' => 5, 'receipt' => E11403)
);
期望的输出:
$purchases_new = array(
array('product_id' => 7, 'receipt' => R13D13),
array('product_id' => 5, 'receipt' => Y28Z14)
);
由于 API 的性质,我只能使用 array_map:
提取数据$purchases_unique = array_unique(
array_map(function($pid){ return $pid['product_id']; }, $purchases)
);
print_r($purchases_unique);
产出
array((array[251] => 7) array([252] => 5))
输出完全不是我想要的:( How to array_map 2 columns product_id'
and receipt
and remove duplicate rows based on product_id
?
我想实现以下目标:
array_map
两列? (product_id
和receipt
)- 删除
product_id
基于product_id
键的重复行?
我认为这会满足您的需求: 只需将唯一的 product_ids(带收据)添加到新数组:
$purchases_new = array_reduce($purchases, function($acc, $p) {
if(!in_array($p['product_id'], array_column($acc, 'product_id'))) {
$acc[] = $p;
}
return $acc;
}, []);
输出:
array (
0 =>
array (
'product_id' => 7,
'receipt' => 'R13D13',
),
1 =>
array (
'product_id' => 5,
'receipt' => 'Y28Z14',
),
)
根据评论 It does not matter if it's the first or last occurrence
,您可以使用 foreach
并为非重复项创建一个 $result
数组。
对于每次迭代,使用product_id
作为数组键并检查它是否已经被设置。
如果不是,则添加。
$purchases = array(
array('product_id' => 7, 'receipt' => "R13D13"),
array('product_id' => 5, 'receipt' => "Y28Z14"),
array('product_id' => 7, 'receipt' => "R02310"),
array('product_id' => 5, 'receipt' => "E11403")
);
$result = [];
foreach($purchases as $key => $purchase) {
if (!isset($result[$purchase["product_id"]])) {
$result[$purchase["product_id"]] = $purchase;
}
}
print_r($result);
结果
Array
(
[7] => Array
(
[product_id] => 7
[receipt] => R13D13
)
[5] => Array
(
[product_id] => 5
[receipt] => Y28Z14
)
)
如果您想要第一次出现,请使用@TheFirstBird 答案。如果你想要最后一次出现,你可以使用 array_column
和 array_values
更简单地购买:
$newArr = array_values(array_column($purchases, null, "product_id"));
注意 array_column
的第二个参数为 null,表示获取整个元素。
实例:3v4l
参考文献:array-column, array-values