PHP 中对数组分组

Sheeraz Gul 2023年1月30日
  1. 在 PHP 中使用 foreach 循环按特定属性对数组进行分组
  2. 在 PHP 中使用 group_array() 创建一个可以对给定数组进行分组的函数
PHP 中对数组分组

PHP 没有任何内置函数可以对数组进行分组,但可以使用循环来实现。

对数组进行分组有助于按单个属性对数组项进行分组。

在 PHP 中使用 foreach 循环按特定属性对数组进行分组

<?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);
?>

输出:

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 ) ) )

我们将阵列排列成多行。输出显示两个数组,一个根据 Gender,另一个是 EmployeeType

在 PHP 中使用 group_array() 创建一个可以对给定数组进行分组的函数

我们可以将上面的代码转换为函数,然后放入值。

<?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));
?>

我们创建一个函数 group_array(),它有两个参数,一个是属性或键,另一个是数组。

输出:

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 ) ) )
作者: 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

相关文章 - PHP Array