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

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)