简体   繁体   English

检查关联数组的特定键中的重复值

[英]Check for repeated values in a particular key of an associative array

I have an associative array having two different prices with the same id. 我有一个关联数组,具有相同ID的两个不同价格。 Let it be... 随它去...

Array ( [0] => Array ( [price] => 3800 [id] => 400015 )  
        [1] => Array ( [price] => 3700 [id] => 400015 )
        [2] => Array ( [price] => 3300 [id] => 400018 )
        [3] => Array ( [price] => 3000 [id] => 400018 )
        [4] => Array ( [price] => 3100 [id] => 400020 )
        [5] => Array ( [price] => 3400 [id] => 400020 ))

I need to display them as 我需要将它们显示为

 id:400015, Price=3800-3700
 id:400018, Price=3000-3600
 id:400020, Price=3100-3400

use below if array_column doesn't support 如果array_column不支持,请在下面使用

$arr = Array ( '0' => Array ( 'price' => 3800, 'id' => 400015 )  ,
        '1' => Array ( 'price' => 3700, 'id' => 400015 ),
        '2' => Array ( 'price' => 3300, 'id' => 400018 ),
        '3' => Array ( 'price' => 3000, 'id' => 400018 ),
        '4' => Array ( 'price' => 3100, 'id' => 400020 ),
        '5' => Array ( 'price' => 3400, 'id' => 400020 ),);

        $new_arr =array();
        foreach($arr as $key=>$row){
            if(isset($new_arr[$row['id']])){
                $new_arr[$row['id']]= $new_arr[$row['id']].'-'.$row['price'];
            }else{
                $new_arr[$row['id']]=$row['price'];
            }
        }

        foreach($new_arr as $key=>$row){
            echo 'id:'.$key.', Price = '.$row.'<br>';
        }

You can loop array and create new array which will be easy to create output you want to show 您可以循环数组并创建新数组,这将很容易创建您想要显示的输出

$newArray = array();
foreach($main as $key=>$val){
    $newArray[$val['id']][] = $val['price'];
}

foreach ($newArray as $key=>$val){
    sort($val,1);
    echo 'id: '.$key.', Price='.implode('-', $val).'<br/>';
}

Loop over the array and print out the respective id and price . 循环遍历数组并打印出相应的idprice Not too sure what you are doing with the price subtraction but this should give you what you need. 不太清楚减价后的处理方式,但这应该可以满足您的需求。

$array = array(array('price'=>3800, 'id'=>40015),
               array('price'=>3700 , 'id'=>40001),
               array('price'=>3800, 'id'=>400015),
               array('price'=>3300 , 'id'=>400018),
               array('price'=>3000 , 'id'=>400018),
               array('price'=>3100 , 'id'=>400020),
               array('price'=>3400 , 'id'=>400020));

asort($array);

foreach ($array as $key => $value)
{
    echo 'id: '.$value['id'].', Price='.$value['price'].'<br />';
}

Just to show a little bit of variance, and use a PHP7 feature: 只是为了显示一些差异,并使用PHP7功能:

usort($data, function($a, $b) { return $a['price'] <=> $b['price']; });
$maxPrices = array_column($data, 'price', 'id');

usort($data, function($a, $b) { return $b['price'] <=> $a['price']; });
$minPrices = array_column($data, 'price', 'id');

foreach($minPrices as $id => $minPrice) {
    echo 'id:', $id, ', Price=', $minPrice, '-', $maxPrices[$id], PHP_EOL;
}

This uses usort() to order the array by maximum price (using the new "spaceship" operator); 这使用usort()按最高价格对数组进行排序(使用新的“ spaceship”运算符); then array_column() to create an array of id's and max prices, as where there are matching id's then the price will be overridden by the previous values so that only the highest value matching each id remains in the $maxPrices array; 然后array_column()创建一个id和最高价格的数组,因为那里有匹配的id,然后价格将被先前的值覆盖,因此只有与每个id匹配的最高值保留在$maxPrices数组中; then does similarly for minimum values. 然后对最小值进行类似的处理。

Finally the loop iterates over the min array, fetching each id and minimum price in turn, and does an id lookup to get the maximum price to display 最后,循环遍历min数组,依次获取每个id和最低价格,并执行id查找以获取要显示的最高价格

Demo 演示版

Note that use of the spaceship operator does require PHP7 请注意,使用飞船运算符确实需要PHP7

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

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