如何在多维数组中组合第一个键值?

时间:2019-04-06 03:37:12

标签: php arrays

我有一个从CSV文件创建的数组,我想使用每个数组中的第一个值作为键,并将数组中具有相同第一个值的数组组合在一起。所有带有[Parent] => top的数组都应合并为一个数组[top] => Array。以下是CSV文件中内容的示例。 MenuItem的数量未知。唯一已知的是,菜单仅深3层进入“顶部”>“子1”>“子2”。顶部菜单的MenuItems也可以更改。我将需要一些可以查看第一个数组中的[Parent]键的东西,如果它等于“ top”,则根据键[MenuItem]创建一个数组,然后再创建任何其他数组,其中[Parent] = Programs然后将[MenuItem]和[URL]添加到Programs数组中。

Array
(
    [0] => Array
        (
            [Parent] => top
            [MenuItem] => Home
            [URL] => /
        )

    [1] => Array
        (
            [Parent] => top
            [MenuItem] => Programs
            [URL] => /programs/
        )

    [2] => Array
        (
            [Parent] => Programs
            [MenuItem] => Programs Sub1
            [URL] => /programs/sub1/
        )

    [3] => Array
        (
            [Parent] => Programs
            [MenuItem] => Programs Sub2
            [URL] => /programs/sub2/
        )

    [4] => Array
        (
            [Parent] => Programs
            [MenuItem] => Programs Sub3
            [URL] => /programs/sub3/
        )
)

结果应为:

Array
(
    [top] => Array
        (
            [0] => Array
                (
                     [MenuItem] => Home
                     [URL] => /
                )
            [1] => Array
                (
                    [MenuItem] => Programs
                    [URL] => /programs/
                    [Programs] => Array
                        (
                            [0] => Array
                                (
                                    [MenuItem] => Programs Sub1
                                    [URL] => /programs/sub1/
                                )
                            [1] => Array
                                (
                                    [MenuItem] => Programs Sub2
                                    [URL] => /programs/sub2/
                                )
                            [2] => Array
                                (
                                    [MenuItem] => Programs Sub3
                                    [URL] => /programs/sub3/
                                )
                )

菜单示例

Home    Programs
            Programs Sub1
            Programs Sub2
            Programs Sub3

1 个答案:

答案 0 :(得分:1)

尝试一下

function loop($arr, $parent, &$result) {
    $temp = array_filter($arr, function($t) use ($parent) {
        return $t['Parent'] == $parent;
    });
    foreach ($temp as $t) {
        $parent = $t['MenuItem'];
        if (array_search($parent, array_column($arr, 'Parent')) !== FALSE) {
            $x = &$result[$t['Parent']][];
            $x = array_slice($t, 1);
            loop($arr, $parent, $x);
        }else{            
            $result[$t['Parent']][] = array_slice($t, 1);
        }
    }
}

$arr = Array
    (Array
        (
        'Parent' => 'top',
        'MenuItem' => 'Home',
        'URL' => '/',
    ),
    Array(
        'Parent' => 'top',
        'MenuItem' => 'Programs',
        'URL' => '/programs/',
    ),
    Array(
        'Parent' => 'Programs',
        'MenuItem' => 'Programs Sub1',
        'URL' => '/programs/sub1/',
    ),
    Array(
        'Parent' => 'Programs',
        'MenuItem' => 'Programs Sub2',
        'URL' => '/programs/sub2/',
    ),
    Array(
        'Parent' => 'Programs',
        'MenuItem' => 'Programs Sub3',
        'URL' => '/programs/sub3/',
    ),
    Array(
        'Parent' => 'Programs Sub2',
        'MenuItem' => 'Programs Sub2 1',
        'URL' => '/programs/sub2/1',
    ),
    Array(
        'Parent' => 'Programs Sub2',
        'MenuItem' => 'Programs Sub2 2',
        'URL' => '/programs/sub2/2',
    )
);

$result = [];

$parent = $arr[0]['Parent'];

loop($arr, $parent, $result);

var_dump($result);

它适用于任何深度的菜单,loop()是一个递归函数。