Skip to content
PHP Logo

How can values in an array be grouped in PHP?

Values in an array can be grouped in PHP by using the array_chunk() function. This function splits an array into chunks of a specified size and returns a multidimensional array. This can be useful for organizing data into smaller, more manageable groups.

php
```php
// Example of grouping values in an array in PHP
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$groupedArray = array_chunk($array, 3);

print_r($groupedArray);
```

This code snippet will group the values in the $array variable into chunks of 3 and store the result in $groupedArray. The print_r() function is then used to display the grouped array.