简体   繁体   中英

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. 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. 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:

<?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:

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_paths = array_intersect_key(array_column($two, 'path', 'sku'), array_column($one, null, 'sku'));
var_export($sku_paths);

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.

$result = [];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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