I want to get the difference between the two arrays in my controller. If I use array_diff()
function, it gives me an error array_diff(): Argument #1 ($array) must be of type array, Illuminate\Support\Collection given
.
$newListOfArray = array_diff($newArrayTags, $oldArrayTags);
Both arrays are definitely arrays.
My $oldArrayTags
array.
#items: array:2 [▼
0 => "egg"
1 => "beef"
]
My $newArrayTags
array.
#items: array:4 [▼
0 => "egg"
1 => "beef"
2 => "chicken"
3 => "sausage"
]
When I checked on Laravel 9 Array Helpers Documentation, I couldn't find any array helpers that will provide the difference.
Questions:
- Why am I getting an error if I use
array_diff
though it was an array? - What is the array helper in Laravel 9 that I can use to get the difference between two arrays?
CodePudding user response:
it looks like a collection, not array you can use collection diff method https://laravel.com/docs/9.x/collections#method-diff
$newArrayTags->diff($oldArrayTags)
or using array_diff if you prefer:
$newListOfArray = array_diff($newArrayTags->toArray(), $oldArrayTags->toArray());