Are there any specific PHP manual chapters or resources that can provide guidance on array manipulation and grouping?
When working with arrays in PHP, the manual chapter on array functions (https://www.php.net/manual/en/ref.array.php) is a valuable resource for guidance on array manipulation and grouping. This chapter provides a comprehensive list of functions that can help with tasks such as sorting, filtering, merging, and grouping arrays.
// Example of grouping an array by a specific key using array_reduce
$data = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30],
['name' => 'Bob', 'age' => 25],
['name' => 'Alice', 'age' => 30]
];
$groupedData = array_reduce($data, function($result, $item) {
$result[$item['age']][] = $item['name'];
return $result;
}, []);
print_r($groupedData);