简体   繁体   English

多维数组比较并创建新数组

[英]Multidimensional array compare and create new array

I have two different array:我有两个不同的数组:

The first:首先:

Array(
 [0] => Array
        (
         [sku] => A
        )
 [1] => Array
        (
         [sku] => B
        )
 [2] => Array
        (
         [sku] => A
        )
)

The second:第二:

Array(
 [0] => Array
        (
         [sku] => A
         [path] => 3
        )
 [1] => Array
        (
         [sku] => B
         [path] => 3
        )
 [2] => Array
        (
         [sku] => C
         [path] => 78
        )
)

In order to i should remove the duplicate in the first array, after that compare both two array, find the same SKU and create a new array with sku and the relative path.为了我应该删除第一个数组中的重复项,然后比较两个数组,找到相同的 SKU 并使用 sku 和相对路径创建一个新数组。 How can I achieve this?我怎样才能做到这一点?

Thanks in advance for the help!在此先感谢您的帮助!

If I got the problem correctly, you need a third array with a list of SKUs and paths for SKUs that exists in the first array.如果我正确地解决了问题,您需要第三个数组,其中包含 SKU 列表和第一个数组中存在的 SKU 的路径。 The following code should do what you're looking for.以下代码应该可以满足您的需求。

<?php

$array1 = [
    [
        'sku' => 'A'
    ],
    [
        'sku' => 'B'
    ],
    [
        'sku' => 'A'
    ]
];

$array2 = [
    [
        'sku'  => 'A',
        'path' => 3
    ],
    [
        'sku'  => 'B',
        'path' => 3
    ],
    [
        'sku'  => 'C',
        'path' => 78
    ]
];

$uniqueSkuList = array_unique(array_map(static function ($el) {
    return $el['sku'];
}, $array1));

$array3 = [];
foreach ($array2 as $item) {
    if (in_array($item['sku'], $uniqueSkuList, true)) {
        $array3[] = $item;
    }
}

var_dump($array3);

You just need to intersect on the skus, assuming you have unique skus in your second array:假设您的第二个数组中有唯一的 skus,您只需要在 skus 上相交:

<?php

$one = 
[
    [
        'sku' => 'A'
    ],
    [
        'sku' => 'B'
    ],
    [
        'sku' => 'A'
    ]
];
$two = 
[
    [
        'sku' => 'A',
        'path' => 3
    ],
    [
        'sku' => 'B',
        'path' => 3
    ],
    [
        'sku' => 'C',
        'path' => 78
    ]
];


$result = array_intersect_key(array_column($two, null, 'sku'), array_column($one, null, 'sku'));
var_export($result);

Output: Output:

array (
    'A' => 
    array (
      'sku' => 'A',
      'path' => 3,
    ),
    'B' => 
    array (
      'sku' => 'B',
      'path' => 3,
    ),
  )

Or to get an array with the sku keys mapping to paths:或者获取一个 sku 键映射到路径的数组:

$sku_paths = array_intersect_key(array_column($two, 'path', 'sku'), array_column($one, null, 'sku'));
var_export($sku_paths);

Output: Output:

array (
  'A' => 3,
  'B' => 3,
)

In addition to the two already existent answers I will describe this more algorithmically, actually implementing the logic instead of solving it with a function , so you can learn from it.除了两个已经存在的答案之外,我将在算法上对此进行更多描述,实际实现逻辑而不是用function解决它,因此您可以从中学习。

$result = [];

foreach ($array1 as $key1 => $value1) {
    foreach ($array2 as $key2 => $value2) {
        if (!isset($result[$value1['sku']])) {
            $result[$value1['sku']] = $value2['path'];
        }
    }
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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