- Saved searches
- Use saved searches to filter your results more quickly
- License
- jakezatecky/array_group_by
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- How to group an array of associative arrays by key in PHP
- Group by function
- Usage
- Group Arrays in PHP
- Use the foreach Loop to Group Array by a Particular Property in PHP
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A PHP function that groups an array by a key or set of keys shared between all array members.
License
jakezatecky/array_group_by
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A PHP function to group an array by a key or set of keys shared between all array members.
To get the latest version of array_group_by , simply require the project using Composer:
$ composer require jakezatecky/array_group_by
Need support for PHP 5.6? Then run the following:
$ composer require jakezatecky/array_group_by:^1.1.0
If you do not want to use Composer, you can just require the src/array_group_by.php file.
To use array_group_by , simply pass an array with any number of keys to group by:
$records = [ [ 'state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus', ], [ 'state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole', ], [ 'state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball', ], [ 'state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb', ], [ 'state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen', ], ]; $grouped = array_group_by($records, 'state', 'city');
Array ( [IN] => Array ( [Indianapolis] => Array ( [0] => Array ( [state] => IN [city] => Indianapolis [object] => School bus ) [1] => Array ( [state] => IN [city] => Indianapolis [object] => Manhole ) ) [Plainfield] => Array ( [0] => Array ( [state] => IN [city] => Plainfield [object] => Basketball ) ) ) [CA] => Array ( [San Diego] => Array ( [0] => Array ( [state] => CA [city] => San Diego [object] => Light bulb ) ) [Mountain View] => Array ( [0] => Array ( [state] => CA [city] => Mountain View [object] => Space pen ) ) ) )
If more complex grouping behavior is desired, you can also pass in a callback function to determine the group key:
$grouped = array_group_by($records, function ($row) < return $row->city; >);
About
A PHP function that groups an array by a key or set of keys shared between all array members.
How to group an array of associative arrays by key in PHP
Learn how to group an array of associative arrays by some key with PHP.
Sometimes, the group by function of SQL won’t be enough to group some data according to your needs. Whatever the reason is, you will be able anyway to group the data as you want with the programming language of your preference. For example, in PHP it’s possible to group an associative array by some key, so you will be able to display it’s data by some order (group).
In this article, we’ll share with you a tiny snippet that allows you to group items of an array by some key.
Group by function
The following function creates a new array that stores all the data of the original input array with an associative structure. It iterates once, creating a new key for the new array with the value of the data with the specified key (so when a new item with the same value appears, it will be pushed into this key). If an item of the input array doesn’t contain the defined key, it will be pushed into the empty string key of the new array:
$key Property to sort by. * @param $data Array that stores multiple associative arrays. */ function group_by($key, $data) < $result = array(); foreach($data as $val) < if(array_key_exists($key, $val))< $result[$val[$key]][] = $val; >else < $result[""][] = $val; >> return $result; >
Usage
Consider the following data structure:
1, "name" => "Bruce Wayne", "city" => "Gotham", "gender" => "Male" ), array( "id" => 2, "name" => "Thomas Wayne", "city" => "Gotham", "gender" => "Male" ), array( "id" => 3, "name" => "Diana Prince", "city" => "New Mexico", "gender" => "Female" ), array( "id" => 4, "name" => "Speedy Gonzales", "city" => "New Mexico", "gender" => "Male" ) );
We have 4 simple items and we want to group them by a single property, for example the gender, so our code to group our data by that key would be:
The dumped array would have the following structure:
array ( 'Male' => array ( 0 => array ( 'id' => 1, 'name' => 'Bruce Wayne', 'city' => 'Gotham', 'gender' => 'Male', ), 1 => array ( 'id' => 2, 'name' => 'Thomas Wayne', 'city' => 'Gotham', 'gender' => 'Male', ), 2 => array ( 'id' => 4, 'name' => 'Speedy Gonzales', 'city' => 'New Mexico', 'gender' => 'Male', ), ), 'Female' => array ( 0 => array ( 'id' => 3, 'name' => 'Diana Prince', 'city' => 'New Mexico', 'gender' => 'Female', ), ), )
As you can see, you will receive a new associative array with all the possible values of the selected key (in this case gender) of your data, so we have 2 groups namely Male and Female. You’ll observe the same logic if you group by another property, for example the city:
array ( 'Gotham' => array ( 0 => array ( 'id' => 1, 'name' => 'Bruce Wayne', 'city' => 'Gotham', 'gender' => 'Male', ), 1 => array ( 'id' => 2, 'name' => 'Thomas Wayne', 'city' => 'Gotham', 'gender' => 'Male', ), ), 'New Mexico' => array ( 0 => array ( 'id' => 3, 'name' => 'Diana Prince', 'city' => 'New Mexico', 'gender' => 'Female', ), 1 => array ( 'id' => 4, 'name' => 'Speedy Gonzales', 'city' => 'New Mexico', 'gender' => 'Male', ), ), )
Group Arrays in PHP
- Use the foreach Loop to Group Array by a Particular Property in PHP
- Use the group_array() to Create a Function That Can Group a Given Array in PHP
PHP doesn’t have any built-in function to group arrays, but it can be achieved using loops.
Grouping an array helps to group array items by a single property.
Use the foreach Loop to Group Array by a Particular Property in PHP
php $data_array = array( array( "Name" => "Mark", "Gender" => "Male", "EmployeeType" => "FullTime" ), array( "Name" => "Monica", "Gender" => "Female", "EmployeeType" => "FullTime" ), array( "Name" => "John", "Gender" => "Male", "EmployeeType" => "PartTime" ), array( "Name" => "Michelle", "Gender" => "Female", "EmployeeType" => "PartTime" ) ); $grouped_array = array(); foreach ($data_array as $element) $grouped_array[$element['Gender']][] = $element; > $grouped_array1 = array(); foreach ($data_array as $element) $grouped_array1[$element['EmployeeType']][] = $element; > echo "Array grouped according to gender:
"; print_r($grouped_array); echo "
Array grouped according to employee type:
"; print_r($grouped_array1); ?>
Array grouped according to gender: Array ( [Male] => Array ( [0] => Array ( [Name] => Mark [Gender] => Male [EmployeeType] => FullTime ) [1] => Array ( [Name] => John [Gender] => Male [EmployeeType] => PartTime ) ) [Female] => Array ( [0] => Array ( [Name] => Monica [Gender] => Female [EmployeeType] => FullTime ) [1] => Array ( [Name] => Michelle [Gender] => Female [EmployeeType] => PartTime ) ) ) Array grouped according to employee type: Array ( [FullTime] => Array ( [0] => Array ( [Name] => Mark [Gender] => Male [EmployeeType] => FullTime ) [1] => Array ( [Name] => Monica [Gender] => Female [EmployeeType] => FullTime ) ) [PartTime] => Array ( [0] => Array ( [Name] => John [Gender] => Male [EmployeeType] => PartTime ) [1] => Array ( [Name] => Michelle [Gender] => Female [EmployeeType] => PartTime ) ) )
We arranged the array in multi-lines. The output shows two arrays, one according to Gender and the other is EmployeeType .