-1

I have two multidimensional array

// Array 1
Array
(
    [0] => Array
        (
            [src] => /wp-content/uploads/2019/09/heythere.jpg
            [source] => database
        )

    [1] => Array
        (
            [src] => /wp-content/uploads/2019/09/test.jpg
            [source] => database
        )

);

// Array 2
Array
(
    [0] => Array
        (
            [src] => /wp-content/uploads/2019/09/heythere.jpg
            [source] => directory
        )

    [1] => Array
        (
            [src] => /wp-content/uploads/2019/09/demo.jpg
            [source] => directory
        )

    [2] => Array
        (
            [src] => /wp-content/uploads/2019/09/image.jpg
            [source] => directory
        )

);

There is similar data in both the arrays like; /wp-content/uploads/2019/09/heythere.jpg

I want to merge these two arrays in one but only unique elements by value also I need to get the element of Array1 if the values are same

So after merging two array I want this result:

Array
(

    [0] => Array
        (
            [src] => /wp-content/uploads/2019/09/demo.jpg
            [source] => directory
        )

    [1] => Array
        (
            [src] => /wp-content/uploads/2019/09/image.jpg
            [source] => directory
        )

    [2] => Array
        (
            [src] => /wp-content/uploads/2019/09/heythere.jpg
            [source] => database
        )

    [3] => Array
        (
            [src] => /wp-content/uploads/2019/09/test.jpg
            [source] => database
        )


);

It will skip "directory" similar element.

Hemant
  • 11
  • 3
  • 2
    The question doesn't appear to include any attempt at all to solve the problem. Please edit the question to show what you've tried, and show a specific roadblock you're running into with a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask) – splash58 Sep 10 '19 at 12:47
  • This is an unattempted, two-part question: 1. merge the two arrays, then 2. [filter duplicate src values](https://stackoverflow.com/q/45603614/2943403) – mickmackusa May 19 '22 at 06:52

2 Answers2

0

You can do it like this:

$array1 = [
    [
        'src' => '/wp-content/uploads/2019/09/heythere.jpg',
        'source' => 'database'
    ],
    [
        'src' => '/wp-content/uploads/2019/09/test.jpg',
        'source' => 'database'
    ]
];

$array2 = [
    [
        'src' => '/wp-content/uploads/2019/09/heythere.jpg',
        'source' => 'directory'
    ],
    [
        'src' => '/wp-content/uploads/2019/09/demo.jpg',
        'source' => 'directory'
    ],
    [
        'src' => '/wp-content/uploads/2019/09/image.jpg',
        'source' => 'directory'
    ]
];

$result = array_merge($array1, $array2);
$uniqueIndexes = array_unique(array_column($result, 'src'));

// remove non unique indexes
foreach ($result as $i => $_) {
    if (!isset($uniqueIndexes[$i])) {
        unset($result[$i]);
    }
}

Sandbox

Claudio
  • 5,078
  • 1
  • 22
  • 33
0

You can use array_merge with array_column and array_values

$u = array_values(array_column(array_merge($a1,$a2), null, 'src'));

Live example :- https://3v4l.org/gkY3a

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20