I have this object $data :
{#792 ▼
+"cities": array:346 [▼
0 => {#454 ▼
+"name": "XXX"
+"id": "1"
}
1 => {#452 ▼
+"name": "ABC"
+"id": "2"
}
1 => {#452 ▼
+"name": "ABC"
+"id": "2"
}
2 => {#433 ▼
+"name": "XYZ"
+"id": "3"
}
etc...
And a string (a text value in the DB) like 1,3.
I want to filter the object with the object that contains that id in the string.
I was thinking to create a collection from $data, then an array from the string:
$dataC = collect ($data);
$array = explode(',', $string)
Create an empty collection and iterate the $dataC, and foreach value from the $dataC that has any of the values from $array, push it to the new empty collection. But for that, I need 2 chained foreachs. Is there a way that I can filter better the object with the values from the first string? Then iterate the array, and for each
citiesis an array already, so you should be able to simply usearray_filter…