首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在PHP中递归地对多个键上的数组进行分组

在PHP中,递归地对多个键上的数组进行分组可以通过以下步骤实现:

  1. 首先,定义一个递归函数,该函数将接收两个参数:待分组的数组和要分组的键名数组。
  2. 在递归函数中,检查待分组的数组是否为空。如果为空,则返回空数组。
  3. 如果待分组的数组不为空,取出第一个键名,并根据该键名对数组进行分组。
  4. 遍历待分组的数组,将具有相同键值的元素放入同一个分组中。
  5. 对每个分组,递归调用该函数,传入剩余的键名数组和该分组作为参数。
  6. 将递归调用的结果与当前分组合并,并返回最终的分组结果。

以下是一个示例代码:

代码语言:txt
复制
function groupArrayByKeys($array, $keys) {
    if (empty($array)) {
        return [];
    }
    
    $currentKey = array_shift($keys);
    $groups = [];
    
    foreach ($array as $item) {
        $key = $item[$currentKey];
        
        if (!isset($groups[$key])) {
            $groups[$key] = [];
        }
        
        $groups[$key][] = $item;
    }
    
    if (!empty($keys)) {
        foreach ($groups as $key => $group) {
            $groups[$key] = groupArrayByKeys($group, $keys);
        }
    }
    
    return $groups;
}

// 示例用法
$data = [
    ['id' => 1, 'category' => 'fruit', 'name' => 'apple'],
    ['id' => 2, 'category' => 'fruit', 'name' => 'orange'],
    ['id' => 3, 'category' => 'vegetable', 'name' => 'carrot'],
    ['id' => 4, 'category' => 'vegetable', 'name' => 'cabbage'],
    ['id' => 5, 'category' => 'fruit', 'name' => 'banana'],
    ['id' => 6, 'category' => 'vegetable', 'name' => 'tomato'],
];

$keys = ['category', 'name'];
$result = groupArrayByKeys($data, $keys);

print_r($result);

上述代码将根据categoryname两个键对数组进行分组。输出结果如下:

代码语言:txt
复制
Array
(
    [fruit] => Array
        (
            [apple] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [category] => fruit
                            [name] => apple
                        )

                )

            [orange] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [category] => fruit
                            [name] => orange
                        )

                )

            [banana] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [category] => fruit
                            [name] => banana
                        )

                )

        )

    [vegetable] => Array
        (
            [carrot] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [category] => vegetable
                            [name] => carrot
                        )

                )

            [cabbage] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [category] => vegetable
                            [name] => cabbage
                        )

                )

            [tomato] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [category] => vegetable
                            [name] => tomato
                        )

                )

        )

)

在这个示例中,我们根据category键和name键对数组进行了分组,最终得到了按照categoryname进行嵌套分组的结果。这个方法可以用于对多个键上的数组进行任意深度的递归分组。

腾讯云相关产品和产品介绍链接地址:

请注意,上述链接仅为腾讯云产品的示例,其他云计算品牌商也提供类似的产品和服务。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

7分8秒

059.go数组的引入

6分33秒

088.sync.Map的比较相关方法

2分25秒

090.sync.Map的Swap方法

1分26秒

夜班睡岗离岗识别检测系统

18分41秒

041.go的结构体的json序列化

16分8秒

Tspider分库分表的部署 - MySQL

7分38秒

人工智能:基于强化学习学习汽车驾驶技术

1分2秒

工程安全监测无线振弦采集仪在隧道中的应用

4分26秒

068.go切片删除元素

5分33秒

JSP 在线学习系统myeclipse开发mysql数据库web结构java编程

14分30秒

Percona pt-archiver重构版--大表数据归档工具

56秒

无线振弦采集仪应用于桥梁安全监测

领券