繁体   English   中英

如果数组列中有多条记录具有相同的值-采取一条记录中另一列的最小值

[英]If there are several records with the same value in array column - take one record with minimum value of other column

我有多维数组看起来像这样:

Array3: [
    0 => array:2 [
            model_id => 1
            price => 2000
         ]      
    1 => array:2 [
            model_id => 2
            price => 3000
         ]
    2 => array:2 [
            model_id => 1
            price => 1500
         ]
   ]

现在,我需要检查model_id的值是否出现多次,如果是,则需要在价格值较低的地方进行选择。 在此示例中,我有两次model_id = 1,所以我应该选择第二个,因为价格最低。

我怎样才能做到这一点? 我试过这种方法:

我如何在php中获取重复的多维数组

PHP:检查多维数组中的重复值

在多维数组中查找重复值

但是我仍然无法解决这个问题。 结果数组应如下所示:

Array2: [
    0 => array:2 [
            model_id => 2
            price => 3000
         ]
    1 => array:2 [
            model_id => 1
            price => 1500
         ]
   ]

简单的foreach应该这样做:

foreach($arr as $e) {
    if (isset($res[$e["model_id"]]))
        $res[$e["model_id"]]["price"] = min($res[$e["model_id"]]["price"], $e["price"]);
    else $res[$e["model_id"]] = $e;
}

直播示例: 3v4l

您可以利用关联数组键来强制唯一性并比较价格:

<?php
// Our data
$array = [
    [ 'model_id' => 1, 'price' => 2000 ],
    [ 'model_id' => 2, 'price' => 3000 ],
    [ 'model_id' => 1, 'price' => 1500 ]
];

// Store the final result
$result = [];

// Loop the data
foreach( $array as $v )
{
    // If this model_id has not been encountered or if the price is lower than what's stored then make it the new price
    if( !isset( $output[ $v[ 'model_id' ] ] ) || $v[ 'price' ] < $output[ $v[ 'model_id' ] ][ 'price' ] )
    {
        $output[ $v[ 'model_id' ] ] = $v;
    }
}

// Get rid of the unique keys
$output = array_values( $output );

print_r( $output );

输出:

Array
(
    [0] => Array
        (
            [model_id] => 1
            [price] => 1500
        )

    [1] => Array
        (
            [model_id] => 2
            [price] => 3000
        )

)

您可以按price降序排序,然后提取并在model_idmodel_id索引,以便最后一个(每个model_id索引的最低price )将覆盖其他的:

array_multisort(array_column($array, 'price'), SORT_DESC, $array);
$result = array_column($array, null, 'model_id');

您可以将它们分组为model_id不是使用min函数

$groupByModelId = [];
foreach($a as $v){
  $groupByModelId[$v['model_id']][] = $v['price'];
}
$searchModelId = 1;
echo min($groupByModelId[$searchModelId]);

工作演示

这是另一种方法,

function array_column1($input, $column_key = null, $index_key = null)
{
    $result = [];
    $i      = 0;
    foreach ($input as $v) {
        $k            = $index_key === null || !isset($v[$index_key]) ? $i++ : $v[$index_key];
        // fetching all the values for model id
        $result[$k][] = $column_key === null ? $v : (isset($v[$column_key]) ? $v[$column_key] : null);
    }
    // fetching minimum for every model_id
    $result = array_map("min", $result);
    return $result;
}
$temp = array_column1($arr, 'price', 'model_id');

演示

我从官方php doc使用了此功能。

您可以使用usort数组函数获取第一个最小值记录,然后使用temp数组并运行foreach函数删除重复的记录。

下面的代码对我来说很好用。

<?php


$arr = array(
        [   
            'model_id' => 1,
            'price' => 2000
        ],
        [
            'model_id' => 2,
            'price' => 3000
        ],
        [
            'model_id' => 1,
            'price' => 1500
        ]
    );

    usort($arr, function ($a, $b) {
        return $a['price'] - $b['price'];
    });

    $input = $arr;
    $temp  = array();
    $keys  = array();

    foreach ( $input as $key => $data ) {
        unset($data['price']);
        if ( !in_array($data, $temp) ) {
            $temp[]     = $data;
            $keys[$key] = true;
        }
    }

    $output = array_intersect_key($input, $keys);

    print_r($output);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM