在php中分组数组元素

时间:2017-01-04 07:16:00

标签: php arrays multidimensional-array

我有这个数组

Array
(
[0] => Array
    (
        [0] => Direct to Exhibitors 
        [1] =>  IMTEX Website
    )

[1] => Array
    (
        [0] => Direct to Exhibitors 
        [1] =>  EDM
    )

[2] => Array
    (
        [0] => Visitors Promotion in General
        [1] => Digital
        [2] => Mobile Apps
    )

[3] => Array
    (
        [0] => Visitor Promotion in General
        [1] => Digital
        [2] => IMTEX Website
    )

[4] => Array
    (
        [0] => Visitor Promotion in Genral
        [1] => Mass Media
        [2] => Print Media
    )

)

我想要这种格式的输出

Array 
(
[Direct to Exhibitors] => Array
    (
        [0] => IMTEX Website
        [1] => EDM
    )

[Visitors Promotion in General] => Array
    (
        [Digital] => Array
            (
                [0] => Mobile Apps
                [1] => IMTEX Website
            )

        [Mass Media] => Array
            (
                [0] => Print Media
            )

    )

)

1 个答案:

答案 0 :(得分:1)

一个简单的foreach将会:

<?php

$arr = [
['Visitors Promotion in General','Digital','Mobile Apps'],
['Visitors Promotion in General','Digital','IMTEX Website'],
['Visitors Promotion in General','Mass Media','Print Media']
];

$finalArr = [];

foreach ($arr as $value) {
    $finalArr[$value[0]][$value[1]][] = $value[2];
}

print_r($finalArr);

<强>打印

Array
(
    [Visitors Promotion in General] => Array
        (
            [Digital] => Array
                (
                    [0] => Mobile Apps
                    [1] => IMTEX Website
                )

            [Mass Media] => Array
                (
                    [0] => Print Media
                )

        )

)

更新1,2:动态尺寸2/3元素

foreach ($arr as $value) {
    if(isset($value[2]))
        $finalArr[$value[0]][$value[1]][] = $value[2];
    else
        $finalArr[$value[0]][] = $value[1];
}

更新3:动态尺寸n个元素

foreach ($arr as $value) {
    $tempArr = [];
    foreach (array_reverse($value) as $arr) {
        $tempArr = [$arr => $tempArr];
    }
    $finalArr = array_replace_recursive($finalArr,$tempArr);
}

[但现在最后一个数组也是一个数组,找到一种方法将其转换为元素]

对于此输入:

$arr = [
['Visitors Promotion in General','Digital','Mobile Apps'],
['Visitors Promotion in General','Digital','Web Apps'],
['Direct to Exhibitors','EDM'],
['Visitors Promotion in General','Digital'],
['Visitors Promotion in General','Mass Media','Print Media'],
['Visitors Promotion in General','Mass Media','Print Media','Mass'],
['Direct to Exhibitors','IMTEX Website']
];

是输出:

Array
(
    [Visitors Promotion in General] => Array
        (
            [Digital] => Array
                (
                    [Mobile Apps] => Array
                        (
                        )

                    [Web Apps] => Array
                        (
                        )

                )

            [Mass Media] => Array
                (
                    [Print Media] => Array
                        (
                            [Mass] => Array
                                (
                                )

                        )

                )

        )

    [Direct to Exhibitors] => Array
        (
            [EDM] => Array
                (
                )

            [IMTEX Website] => Array
                (
                )

        )

)

尝试使用此解决方法:

function findEmpty($arr) {
    foreach ($arr as $key => $value) {
        if(is_array($value) && !empty($value)){
            $arr[$key] = findEmpty($value);
        } else {
            return $key;
        }
    }
    return $arr;
}

print_r(findEmpty($finalArr));