合并两个数组,根据其属性保留合并元素的一个版本

时间:2018-10-18 11:08:03

标签: php arrays

下面是三个数组:

    $one = array (
        array (
            'supplier_name' => 'SUP1',
            'product_code' => 'ITM001',
            'product' => 'Book',
            'price' => '5500',
        ),
        array (
            'supplier_name' => 'SUP1',
            'product_code' => 'ITM002',
            'product' => 'Pencil',
            'price' => '1500',
        ),
        array (
            'supplier_name' => 'SUP1',
            'product_code' => 'ITM002',
            'product' => 'Eraser',
            'price' => '1750',
        ),
    )

    $two = array (
        array (
            'supplier_name' => 'SUP2',
            'product_code' => 'SP001',
            'product' => 'Book',
            'price' => '5250',
        ),
        array (
            'supplier_name' => 'SUP2',
            'product_code' => 'SP002',
            'product' => 'Pencil',
            'price' => '1550',
        ),
        array (
            'supplier_name' => 'SUP2',
            'product_code' => 'SP002',
            'product' => 'Ruler',
            'price' => '2300',
        ),
    )

    $three = array (
        array (
            'supplier_name' => 'SUP3',
            'product_code' => 'BRG01',
            'product' => 'Book',
            'price' => '5250',
        ),
        array (
            'supplier_name' => 'SUP3',
            'product_code' => 'BRG02',
            'product' => 'Ruler',
            'price' => '2350',
        ),
        array (
            'supplier_name' => 'SUP3',
            'product_code' => 'BRG02',
            'product' => 'Drawing book',
            'price' => '4500',
        ),
    )

我已经将上述所有数组与array_merge合并了。

$all_array = array (
  0 => 
  array (
    'supplier_name' => 'SUP1',
    'product_code' => 'ITM001',
    'product' => 'Book',
    'price' => '5500',
  ),
  1 => 
  array (
    'supplier_name' => 'SUP1',
    'product_code' => 'ITM002',
    'product' => 'Pencil',
    'price' => '1500',
  ),
  2 => 
  array (
    'supplier_name' => 'SUP1',
    'product_code' => 'ITM002',
    'product' => 'Eraser',
    'price' => '1750',
  ),
  3 => 
  array (
    'supplier_name' => 'SUP2',
    'product_code' => 'SP001',
    'product' => 'Book',
    'price' => '5250',
  ),
  4 => 
  array (
    'supplier_name' => 'SUP2',
    'product_code' => 'SP002',
    'product' => 'Pencil',
    'price' => '1550',
  ),
  5 => 
  array (
    'supplier_name' => 'SUP2',
    'product_code' => 'SP002',
    'product' => 'Ruler',
    'price' => '2300',
  ),
  6 => 
  array (
    'supplier_name' => 'SUP3',
    'product_code' => 'BRG01',
    'product' => 'Book',
    'price' => '5250',
  ),
  7 => 
  array (
    'supplier_name' => 'SUP3',
    'product_code' => 'BRG02',
    'product' => 'Ruler',
    'price' => '2350',
  ),
  8 => 
  array (
    'supplier_name' => 'SUP3',
    'product_code' => 'BRG02',
    'product' => 'Drawing book',
    'price' => '4500',
  ),
)

如何删除具有较高价值的重复项,而仅降低所有项目的价格。

Array
(
    [0] => Array
        (
            [supplier_name] => SUP2
            [product_code] => SP001
            [product] => Book
            [price] => 5250
        )

    [1] => Array
        (
            [supplier_name] => SUP1
            [product_code] => ITM002
            [product] => Pencil
            [price] => 1500
        )

    [2] => Array
        (
            [supplier_name] => SUP1
            [product_code] => ITM002
            [product] => Eraser
            [price] => 1750
        )

    [3] => Array
        (
            [supplier_name] => SUP2
            [product_code] => SP002
            [product] => Ruler
            [price] => 2300
        )

    [4] => Array
        (
            [supplier_name] => SUP3
            [product_code] => BRG02
            [product] => Drawing book
            [price] => 4500
        )

)

请告知。谢谢。

1 个答案:

答案 0 :(得分:0)

似乎您正在使用PHP。尽管使用唯一的命名密钥映射产品会更好,也不会那么痛苦,但这是解决问题的一种方法。

请勿使用array_merge,因为它不允许您使用回调仅保留便宜的产品。

这里是您可以使用的伪代码(而不是我将提供的未经测试的php)。使其适应您的源代码。

{
   objA: hi
}
{
  objB1: world
  objB2: !  
}