PHP 中對陣列分組

Sheeraz Gul 2023年1月30日 2022年5月13日
  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 ) ) )
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

相關文章 - PHP Array