What are the advantages of using combined keys for grouping arrays in PHP?

When grouping arrays in PHP, using combined keys allows for more flexibility and precision in how the arrays are grouped. By combining multiple keys into a single key, you can create unique groupings that may not be possible with a single key alone. This can be especially useful when dealing with multidimensional arrays or when you need to group arrays based on multiple criteria.

// Sample array to group
$items = [
    ['name' => 'apple', 'color' => 'red', 'type' => 'fruit'],
    ['name' => 'banana', 'color' => 'yellow', 'type' => 'fruit'],
    ['name' => 'carrot', 'color' => 'orange', 'type' => 'vegetable'],
    ['name' => 'blueberry', 'color' => 'blue', 'type' => 'fruit'],
    ['name' => 'lettuce', 'color' => 'green', 'type' => 'vegetable'],
];

// Grouping the array by combining 'color' and 'type' keys
$groupedItems = [];
foreach ($items as $item) {
    $key = $item['color'] . '_' . $item['type'];
    $groupedItems[$key][] = $item;
}

// Output the grouped array
print_r($groupedItems);