3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ { "name": "D\'Amore Ltd", "id": 67, "catId": 1 }, { "name": "D\'Amore Ltd", "id": 67, "catId": 2 }, { "name": "D\'Amore Ltd", "id": 67, "catId": 3 }, { "name": "Kuvalis, Denesik and Terry", "id": 14, "catId": 1 }, { "name": "Labadie-Bauch", "id": 1, "catId": 1 }, { "name": "Labadie-Bauch", "id": 1, "catId": 2 } ]'; $locations = json_decode($json); $categories = [1, 2]; $collection = []; $grouped = array_reduce($locations, function ($grouped, $location) { $grouped[$location->name][] = $location->catId; return $grouped; }, []); $grouped = array_filter($grouped, function ($_categories) use ($categories) { return ! array_diff($categories, $_categories); }); foreach ($locations as $location) { if (isset($grouped[$location->name]) && in_array($location->catId, $categories)) { $collection[] = $location; } } var_dump($collection);

preferences:
32.22 ms | 409 KiB | 5 Q