如何在具有最小值和组 PHP 的关联数组中逐一获取元素
How to get one by one elements in associative array with min value and group PHP
我有这样的关联数组
Array
(
[0] => Array
(
[id] => 1,
[product_uname] => AI 92
[price_per_l] => 44.82
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.43
)
[2] => Array
(
[id] => 3
[product_uname] => AI 95
[price_per_l] => 46.35
)
[3] => Array
(
[id] => 4
[product_uname] => AI 92
[price_per_l] => 44.19
)
[4] => Array
(
[id] => 5
[product_uname] => AI 100
[price_per_l] => 53.03
)
)
我需要过滤数组,我们按 ['product_uname'] 字段分组,并在仅分组一个具有最低价格 [price_per_l] 字段的唯一元素后提取,并按 [[=21] 对数组排序=]] 字段之后,我需要查看
Array
(
[0] => Array
(
[id] => 1
[product_uname] => AI 92
[price_per_l] => 44.19 (min value price where product_uname = AI 92)
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.35 (min value price where product_uname = AI 95)
)
[2] => Array
(
[id] => 3
[product_uname] => AI 100
[price_per_l] => 53.03 (min value price because there is no any product_name = AI 100)
)
)
是否可以不循环创建
我会这样做:
<?php
$new_array = array();
foreach($original_array as $key=>$item) {
if(!isset($new_array[$item['product_uname']])) {
//There isn't an item for this group in array, save this
$new_array[$item['product_uname']] = $item;
} else {
if($new_array[$item['product_uname']]['price_per_l'] > $item['price_per_l']) {
//If current item of group has higher price, replace it with this
$new_array[$item['product_uname']] = $item;
}
}
}
如果您按 price_per_l
降序排序,然后按 product_uname
对数组进行索引,那么您将只得到最低的,因为后面的项目将取代前面的项目,而后面的项目价格最低:
array_multisort(array_column($array, 'price_per_l'), SORT_DESC, $array);
$array = array_column($array, null, 'product_uname');
如果需要,您可以重新编制索引:
$array = array_values($array);
$array = Array
(
[0] => Array
(
[id] => 1,
[product_uname] => AI 92
[price_per_l] => 44.82
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.43
)
[2] => Array
(
[id] => 3
[product_uname] => AI 95
[price_per_l] => 46.35
)
[3] => Array
(
[id] => 4
[product_uname] => AI 92
[price_per_l] => 44.19
)
[4] => Array
(
[id] => 5
[product_uname] => AI 100
[price_per_l] => 53.03
)
);
array_multisort(array_column($array, 'price_per_l'), SORT_ASC, $array);
$array = array_column($array, null, 'product_uname');
$array = array_values($array);
我有这样的关联数组
Array
(
[0] => Array
(
[id] => 1,
[product_uname] => AI 92
[price_per_l] => 44.82
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.43
)
[2] => Array
(
[id] => 3
[product_uname] => AI 95
[price_per_l] => 46.35
)
[3] => Array
(
[id] => 4
[product_uname] => AI 92
[price_per_l] => 44.19
)
[4] => Array
(
[id] => 5
[product_uname] => AI 100
[price_per_l] => 53.03
)
)
我需要过滤数组,我们按 ['product_uname'] 字段分组,并在仅分组一个具有最低价格 [price_per_l] 字段的唯一元素后提取,并按 [[=21] 对数组排序=]] 字段之后,我需要查看
Array
(
[0] => Array
(
[id] => 1
[product_uname] => AI 92
[price_per_l] => 44.19 (min value price where product_uname = AI 92)
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.35 (min value price where product_uname = AI 95)
)
[2] => Array
(
[id] => 3
[product_uname] => AI 100
[price_per_l] => 53.03 (min value price because there is no any product_name = AI 100)
)
)
是否可以不循环创建
我会这样做:
<?php
$new_array = array();
foreach($original_array as $key=>$item) {
if(!isset($new_array[$item['product_uname']])) {
//There isn't an item for this group in array, save this
$new_array[$item['product_uname']] = $item;
} else {
if($new_array[$item['product_uname']]['price_per_l'] > $item['price_per_l']) {
//If current item of group has higher price, replace it with this
$new_array[$item['product_uname']] = $item;
}
}
}
如果您按 price_per_l
降序排序,然后按 product_uname
对数组进行索引,那么您将只得到最低的,因为后面的项目将取代前面的项目,而后面的项目价格最低:
array_multisort(array_column($array, 'price_per_l'), SORT_DESC, $array);
$array = array_column($array, null, 'product_uname');
如果需要,您可以重新编制索引:
$array = array_values($array);
$array = Array
(
[0] => Array
(
[id] => 1,
[product_uname] => AI 92
[price_per_l] => 44.82
)
[1] => Array
(
[id] => 2
[product_uname] => AI 95
[price_per_l] => 46.43
)
[2] => Array
(
[id] => 3
[product_uname] => AI 95
[price_per_l] => 46.35
)
[3] => Array
(
[id] => 4
[product_uname] => AI 92
[price_per_l] => 44.19
)
[4] => Array
(
[id] => 5
[product_uname] => AI 100
[price_per_l] => 53.03
)
);
array_multisort(array_column($array, 'price_per_l'), SORT_ASC, $array);
$array = array_column($array, null, 'product_uname');
$array = array_values($array);