迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > PHP >

PHP 中对数组分组

作者:迹忆客 最近更新:2023/03/15 浏览次数:

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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 PHP 中获取时间差的分钟数

发布时间:2023/03/29 浏览次数:183 分类:PHP

本文介绍了如何在 PHP 中获取时间差的分钟数,包括 date_diff()函数和数学公式。它包括 date_diff()函数和数学公式。

PHP 中的重定向

发布时间:2023/03/29 浏览次数:136 分类:PHP

本教程演示了如何将用户从页面重定向到 PHP 中的其他页面

PHP 分页

发布时间:2023/03/29 浏览次数:66 分类:PHP

本教程介绍如何在 PHP 中对数据库行进行分页

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便