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



我有三个数组作为波纹管:

$one = 数组 ( 阵列 ( "supplier_name" => "SUP1", 'product_code' => 'ITM001', "产品" => "书", '价格' => '5500', ), 阵列 ( "supplier_name" => "SUP1", 'product_code' => 'ITM002', "产品" => "铅笔", "价格" => "1500", ), 阵列 ( "supplier_name" => "SUP1", 'product_code' => 'ITM002', "产品" => "橡皮擦", '价格' => '1750', ), ) $two = 数组 ( 阵列 ( "supplier_name" => "SUP2", "product_code" => "SP001", "产品" => "书", '价格' => '5250', ), 阵列 ( "supplier_name" => "SUP2", "product_code" => "SP002", "产品" => "铅笔", '价格' => '1550', ), 阵列 ( "supplier_name" => "SUP2", "product_code" => "SP002", "产品" => "标尺", '价格' => '2300', ), ) $three = 数组 ( 阵列 ( 'supplier_name' => 'SUP3', 'product_code' => 'BRG01', "产品" => "书", '价格' => '5250', ), 阵列 ( 'supplier_name' => 'SUP3', 'product_code' => 'BRG02', "产品" => "标尺", '价格' => '2350', ), 阵列 ( 'supplier_name' => 'SUP3', 'product_code' => 'BRG02', "产品" => "绘图书", '价格' => '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
)
)

请指教。谢谢。

看来你正在使用PHP。尽管使用唯一的命名键映射您的产品会更好,更少痛苦,但这里有一种方法可以解决您的问题。

不要使用array_merge因为它不允许您使用回调来仅保留更便宜的产品。

这是一个你可以使用的伪代码(而不是我会提供的未经测试的php(。使其适应您的源代码。

// Everything has a beginning.
array_result = array()
// Browsing array1
For each item of array1
found_item = false
// For each product of array1, seeking for the same product in array2
For each item2 of array2
If areTheSameProduct(item1, item2)
If item1.price < item2.price Then
appendToArray(array_result, item1)
Else
appendToArray(array_result, item2)
End If
found_item = true
// Removing then the product in array2, to let at the end only
// the ones which was't found in array1.
// In PHP, use here unset() to remove an element from the array.
// "key" parameter can be a named index or the classic integer one
unset(array1[key of item2])
Break // No need to continue the loop on array2
End If
End For
// Item not found in array2? We'll keep the one of array1.
If (Not found_item) Then appendToArray(array_result, item1)
End For
// For the remaining values of array_two (which were not in array1)
For each item2 of array2
appendToArray(array_result, item2 )
End For
// Comparator function
Function areTheSameProduct(item1, item2)
return (item1.supplier_name == item2.supplier_name) AND
(item1.product_code  == item2.product_code) AND
(item1.product       == item2.product)
End Function

最新更新