dongwei7245 2018-09-03 11:06
浏览 98
已采纳

找到重复的值并比较另一个键

I need a advice from you. I have an array and I need to find arrays with same value of key. Then I need to compare another key of founded arrays and remove array which has lower value of key.

Example below.

As you can see, there are two arrays with same EAN key. I need to find arrays with same EAN. Then compare these two array by key ProductCount. The array with a higer ProdouctCount should be removed. Do you understand?

[20] => Array
    (
        [ean] => **6900532615069**
        [productPrice] => 1140
        [productCount] => 50
    )
[25] => Array
    (
        [ean] => 6900535364122
        [productPrice] => 1140
        [productCount] => 50
    )

[36] => Array
    (
        [ean] => **6900532615069**
        [productPrice] => 1140
        [productCount] => 10
    )

function removeduplicateKeys($data){

$_data = array();

foreach ($data as $v) {
  if (isset($_data[$v['ean']])) {
    // found duplicate
    continue;
  }
  // remember unique item
  $_data[$v['ean']] = $v;
}

$data = array_values($_data);
return $data;

}

So output should be

[25] => Array
    (
        [ean] => 6900535364122
        [productPrice] => 1140
        [productCount] => 50
    )

[36] => Array
    (
        [ean] => **6900532615069**
        [productPrice] => 1140
        [productCount] => 10
    )

I am trying to do it for about three days but I do not how. The farthest thing I did was deleting a duplicate array but I don't know how to compare a key value and then delete the array. I would be grateful for any advice. Thank you.

  • 写回答

1条回答 默认 最新

  • doujian4752 2018-09-03 11:11
    关注

    You can use array_column to make the array associative.
    This means it will overwrite any duplicate arrays also.
    Then just array_values to set it back to original indexed keys.

    $arr = array_values(array_column($arr, NULL, "ean"));
    

    Edit: I see that you want keys 25 and 36.
    The code above will give you 20 and 25.

    To get your expected result you need to rsort the array first to make it backwards.

    rsort($arr);
    $arr = array_values(array_column($arr, NULL, "ean"));
    


    The array_column will create an array like this:

    [**6900532615069**] => Array
        (
            [ean] => **6900532615069**
            [productPrice] => 1140
            [productCount] => 50
        )
    [6900535364122] => Array
        (
            [ean] => 6900535364122
            [productPrice] => 1140
            [productCount] => 50
        )
    
    [**6900532615069**] => Array
        (
            [ean] => **6900532615069**
            [productPrice] => 1140
            [productCount] => 10
        )
    

    But since there can only be one array with the same key the second array will overwrite the first giving:

    [6900535364122] => Array
        (
            [ean] => 6900535364122
            [productPrice] => 1140
            [productCount] => 50
        )
    
    [**6900532615069**] => Array
        (
            [ean] => **6900532615069**
            [productPrice] => 1140
            [productCount] => 10
        )
    

    If you use the rsort() first it will remove the other array instead.
    Array_values will then remove the "ean" from the array make it 0,1,2...

    working code https://3v4l.org/sfPQr


    if the array is unsorted you need to sort the array first on productcount.

    usort($arr, function ($a, $b) {
        return $b['productCount'] - $a['productCount'];
    });
    
    
    $arr = array_values(array_column($arr, NULL, "ean"));
    var_dump($arr);
    

    https://3v4l.org/WZKLB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址