Group Arrays in PHP

Sheeraz Gul Feb 27, 2022 PHP PHP Array
  1. Use the foreach Loop to Group Array by a Particular Property in PHP
  2. Use the group_array() to Create a Function That Can Group a Given Array in PHP
Group Arrays 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: <br>";
print_r($grouped_array);
echo "<br><br>Array grouped according to employee type: <br>";
print_r($grouped_array1);
?>

Output:

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.

Use the group_array() to Create a Function That Can Group a Given Array in PHP

We can convert the code above into a function and then put the values.

<?php
function group_array($property, $data) {
    $grouped_array = array();

    foreach($data as $value) {
        if(array_key_exists($property, $value)){
            $grouped_array[$value[$property]][] = $value;
        }else{
            $grouped_array[""][] = $value;
        }
    }

    return $grouped_array;
}
echo "Array grouped according to gender: <br>";
print_r(group_array("Gender", $data_array));
echo "<br><br>Array grouped according to employee type: <br>";
print_r(group_array("EmployeeType", $data_array));
?>

We create a function group_array(), which takes two parameters, one is the property or key, and the other is the array.

Output:

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 ) ) )
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Array

  • Determine the First and Last Iteration in a Foreach Loop in PHP
  • Convert an Array to a String in PHP
  • Get the First Element of an Array in PHP
  • Echo or Print an Array in PHP
  • Delete an Element From an Array in PHP
  • Remove Empty Array Elements in PHP