How can one retrieve all items of a specific brand from an array in PHP, rather than just the first occurrence?

To retrieve all items of a specific brand from an array in PHP, you can loop through the array and collect all items that match the specified brand into a new array. This can be achieved by using a foreach loop to iterate over the original array and checking if the brand of each item matches the desired brand. If a match is found, the item is added to a new array that will store all items of the specified brand.

$items = [
    ['brand' => 'Nike', 'name' => 'Shoes'],
    ['brand' => 'Adidas', 'name' => 'Shirt'],
    ['brand' => 'Nike', 'name' => 'Shorts'],
    ['brand' => 'Puma', 'name' => 'Hat'],
];

$brand = 'Nike';
$filteredItems = [];

foreach ($items as $item) {
    if ($item['brand'] === $brand) {
        $filteredItems[] = $item;
    }
}

print_r($filteredItems);