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
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.
Basically you just get all the names first (via
array_column
) and make em flat, so that you can utilizein_array
and make your search and comparison.Then, it’s just a humble
foreach
andif
at that point. This example creates a new copy of the difference. If you prefer not to create another copy, you can justunset()
the first array and reverse the condition, leaving only the difference on the original.array_udiff can help: