skip to Main Content

I am Working in Magento 2.3 i have two arrays

First Array:

Array
(
    [0] => Array
        (
            [name] => /l/u/luma-foam-roller.jpg
            [image_id] => luma-foam-roller.jpg
        )

    [1] => Array
        (
            [name] => /l/u/luma-yoga-strap-set.jpg
            [image_id] => luma-yoga-strap-set.jpg
        )

    [2] => Array
        (
            [name] => /m/b/mb04-black-0.jpg
            [image_id] => mb04-black-0.jpg
        )

    [3] => Array
        (
            [name] => /l/u/luma-stability-ball-pink.jpg
            [image_id] => luma-stability-ball-pink.jpg
        )

    [4] => Array
        (
            [name] => /m/b/mb01-blue-0.jpg
            [image_id] => mb01-blue-0.jpg
        )
}

Second Array:

Array
(
    [0] => Array
        (
            [name] => /m/b/mb01-blue-0.jpg
        )

    [1] => Array
        (
            [name] => /m/b/mb04-black-0.jpg
        )

    [2] => Array
        (
            [name] => /m/b/mb04-black-0_alt1.jpg
        )

    [3] => Array
        (
            [name] => /m/b/mb03-black-0.jpg
        )

    [4] => Array
        (
            [name] => /m/b/mb03-black-0_alt1.jpg
        )
}

i want to find difference on the basis of name
Expected Result for me is

Array
(
    [0] => Array
        (
            [name] => /l/u/luma-foam-roller.jpg
            [image_id] => luma-foam-roller.jpg
        )

    [1] => Array
        (
            [name] => /l/u/luma-yoga-strap-set.jpg
            [image_id] => luma-yoga-strap-set.jpg
        )


    [3] => Array
        (
            [name] => /l/u/luma-stability-ball-pink.jpg
            [image_id] => luma-stability-ball-pink.jpg
        )

}

i tried Following Functions But Non of them are working for me

  • array_diff()
  • array_diff_assoc()
  • array_unique(array_merge($array1,$array2), SORT_REGULAR);

but not getting any success

2

Answers


  1. I can’t think of any one liner function that does this automatically at this time, but one straightforward way is just to use a loop.

    $names = array_column($second, 'name');
    $diff = array();
    foreach ($first as $k => $values) {
        if (!in_array($values['name'], $names)) {
            $diff[$k] = $values;
        }
    }
    // print_r($diff);
    

    Basically you just get all the names first (via array_column) and make em flat, so that you can utilize in_array and make your search and comparison.

    Then, it’s just a humble foreach and if at that point. This example creates a new copy of the difference. If you prefer not to create another copy, you can just unset() the first array and reverse the condition, leaving only the difference on the original.

    Login or Signup to reply.
  2. array_udiff can help:

    function compare_by_name($a, $b) {
        $nameA = isset($a['name']) ? $a['name'] : '';
        $nameB = isset($b['name']) ? $b['name'] : '';
    
        return strcmp($nameA, $nameB);
    }
    
    print_r(array_udiff($arr1, $arr2, 'compare_by_name'));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search