简体   繁体   中英

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. 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

$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 . 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:

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); 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; 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

Demo

Note that use of the spaceship operator does require PHP7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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