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

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


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 所以我应该选择第二个,因为价格最低。

我怎样才能做到这一点?

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


Array2: [

    0 => array:2 [

            model_id => 2

            price => 3000

         ]

    1 => array:2 [

            model_id => 1

            price => 1500

         ]

   ]


茅侃侃
浏览 157回答 3
3回答

绝地无双

您可以利用关联数组键来强制唯一性并比较价格:<?php// Our data$array = [&nbsp; &nbsp; [ 'model_id' => 1, 'price' => 2000 ],&nbsp; &nbsp; [ 'model_id' => 2, 'price' => 3000 ],&nbsp; &nbsp; [ 'model_id' => 1, 'price' => 1500 ]];// Store the final result$result = [];// Loop the dataforeach( $array as $v ){&nbsp; &nbsp; // If this model_id has not been encountered or if the price is lower than what's stored then make it the new price&nbsp; &nbsp; if( !isset( $output[ $v[ 'model_id' ] ] ) || $v[ 'price' ] < $output[ $v[ 'model_id' ] ][ 'price' ] )&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $output[ $v[ 'model_id' ] ] = $v;&nbsp; &nbsp; }}// Get rid of the unique keys$output = array_values( $output );print_r( $output );输出:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [model_id] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 1500&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [model_id] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [price] => 3000&nbsp; &nbsp; &nbsp; &nbsp; ))

慕无忌1623718

您可以按降序排序price,然后提取和索引,model_id以便最后一个(price每个model_id索引的最低值)将覆盖其他:array_multisort(array_column($array, 'price'), SORT_DESC, $array);$result = array_column($array, null, 'model_id');

12345678_0001

您可以使用 usort 数组函数获取第一个最小值记录,然后使用临时数组并运行 foreach 函数来删除重复记录。下面的代码对我来说工作正常。<?php$arr = array(&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model_id' => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'price' => 2000&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model_id' => 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'price' => 3000&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'model_id' => 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'price' => 1500&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; );&nbsp; &nbsp; usort($arr, function ($a, $b) {&nbsp; &nbsp; &nbsp; &nbsp; return $a['price'] - $b['price'];&nbsp; &nbsp; });&nbsp; &nbsp; $input = $arr;&nbsp; &nbsp; $temp&nbsp; = array();&nbsp; &nbsp; $keys&nbsp; = array();&nbsp; &nbsp; foreach ( $input as $key => $data ) {&nbsp; &nbsp; &nbsp; &nbsp; unset($data['price']);&nbsp; &nbsp; &nbsp; &nbsp; if ( !in_array($data, $temp) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp[]&nbsp; &nbsp; &nbsp;= $data;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $keys[$key] = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $output = array_intersect_key($input, $keys);&nbsp; &nbsp; print_r($output);
打开App,查看更多内容
随时随地看视频慕课网APP