PHP - 多维数组到一维数组

问题描述 投票:0回答:1

我一直在尝试将多维数组转换为单维数组。我尝试过使用 PHP 数组内置函数,但无法获得所需的输出。

问题 我有类别层次结构的多维数组,它将子类别存储在“children”键中。儿童级别没有限制,可以是任意数量。

结果 我正在尝试将此多维数组转换为单维数组,但子数组应添加在其父数组之后,并具有额外的键“级别”来显示层次结构顺序。

多维数组




$arr = [
    [
        "id" => 1495,
        "title" => "Acoustics",
        "slug" => "acoustics",
        "children" => [
            [
                "id" => 50127,
                "title" => "Acoustic Lighting",
                "slug" => "acoustic-lighting",
                "children" => [
                    [
                        "id" => 50131,
                        "title" => "Acoustic Pendant Lighting",
                        "slug" => "acoustic-pendant-lighting",
                        "children" => [],
                    ],
                ],
            ],
            [
                "id" => 57168,
                "title" => "Acoustic Plaster Systems",
                "slug" => "acoustic-plaster-systems",
                "children" => [],
            ],
        ],
    ],
    [
        "id" => 50182,
        "title" => "Architecture",
        "slug" => "architecture",
        "children" => [
            [
                "id" => 1488,
                "title" => "Ceilings",
                "slug" => "ceilings",
                "children" => [
                    [
                        "id" => 50205,
                        "title" => "Suspended Ceiling Systems",
                        "slug" => "suspended-ceiling-systems",
                        "children" => [
                            [
                                "id" => 56299,
                                "title" => "Baffles",
                                "slug" => "baffles",
                                "children" => [],
                            ],
                        ],
                    ],
                ],
            ],
            [
                "id" => 50187,
                "title" => "Doors",
                "slug" => "doors",
                "children" => [
                    [
                        "id" => 50188,
                        "title" => "Single Leaf",
                        "slug" => "single-leaf",
                        "children" => [],
                    ],
                ],
            ],
        ],
    ],
    [
        "id" => 43859,
        "title" => "Decor",
        "slug" => "decor",
        "children" => [
            [
                "id" => 50137,
                "title" => "Art and Prints",
                "slug" => "art-and-prints",
                "children" => [],
            ],
            [
                "id" => 54014,
                "title" => "Planters",
                "slug" => "planters",
                "children" => [],
            ],
            [
                "id" => 50138,
                "title" => "Sculptures and Objects",
                "slug" => "sculptures-and-objects",
                "children" => [],
            ],
            [
                "id" => 43860,
                "title" => "Wall / Ceiling Decor",
                "slug" => "wall-ceiling-decor",
                "children" => [
                    [
                        "id" => 43861,
                        "title" => "Green Walls",
                        "slug" => "green-walls",
                        "children" => [],
                    ],
                    [
                        "id" => 50139,
                        "title" => "Wall Paper & Wall Coverings",
                        "slug" => "wall-paper-wall-coverings",
                        "children" => [],
                    ],
                ],
            ],
        ],
    ],
];

一维数组,我正在尝试获取


Array
(
    [0] => Array
        (
            [id] => 1495
            [title] => Acoustics
            [slug] => acoustics
            [level] => 0
        )

    [1] => Array
        (
            [id] => 50127
            [title] => Acoustic Lighting
            [slug] => acoustic-lighting
            [level] => 1
        )

    [2] => Array
        (
            [id] => 50131
            [title] => Acoustic Pendant Lighting
            [slug] => acoustic-pendant-lighting
            [level] => 2
        )

    [3] => Array
        (
            [id] => 57168
            [title] => Acoustic Plaster Systems
            [slug] => acoustic-plaster-systems
            [level] => 1
        )

    [4] => Array
        (
            [id] => 50182
            [title] => Architecture
            [slug] => architecture
            [level] => 0
        )

    [5] => Array
        (
            [id] => 1488
            [title] => Ceilings
            [slug] => ceilings
            [level] => 1
        )

    [6] => Array
        (
            [id] => 50205
            [title] => Suspended Ceiling Systems
            [slug] => suspended-ceiling-systems
            [level] => 2
        )

    [7] => Array
        (
            [id] => 56299
            [title] => Baffles
            [slug] => baffles
            [level] => 3
        )

    [8] => Array
        (
            [id] => 50187
            [title] => Doors
            [slug] => doors
            [level] => 1
        )

    [9] => Array
        (
            [id] => 50188
            [title] => Single Leaf
            [slug] => single-leaf
            [level] => 2
        )

    [10] => Array
        (
            [id] => 43859
            [title] => Decor
            [slug] => decor
            [level] => 0
        )

    [11] => Array
        (
            [id] => 50137
            [title] => Art and Prints
            [slug] => art-and-prints
            [level] => 1
        )

    [12] => Array
        (
            [id] => 54014
            [title] => Planters
            [slug] => planters
            [level] => 1
        )

    [13] => Array
        (
            [id] => 50138
            [title] => Sculptures and Objects
            [slug] => sculptures-and-objects
            [level] = 1
        )

    [14] => Array
        (
            [id] => 43860
            [title] => Wall / Ceiling Decor
            [slug] => wall-ceiling-decor
            [level] => 1
        )

    [15] => Array
        (
            [id] => 43861
            [title] => Green Walls
            [slug] => green-walls
            [level] => 2
        )

    [16] => Array
        (
            [id] => 50139
            [title] => Wall Paper & Wall Coverings
            [slug] => wall-paper-wall-coverings
            [level] => 2
        )

)

这是我尝试过的,

function array_flatten( $arr, $out=array() )  {
    foreach( $arr as $key => $item ) {
        if ( is_array( $item )  ) {
            $out = array_merge( $out, array_flatten( $item ) );
        } else {
            $out[] = $item;
        }
    }
    return $out;
}

$result_single = array_flatten($arr);
$result_arr = array_chunk($result_single, 3);
echo "<pre>"; print_r($result_arr); echo "</pre>";

php arrays multidimensional-array
1个回答
0
投票

你需要使用递归

function flattenArray($array, $level = 0, &$result = []) {
    foreach ($array as $element) {
        // Copy the element and add the level key to it
        $flattenedElement = $element;
        $flattenedElement['level'] = $level;
        unset($flattenedElement['children']); // Remove children key

        // Add the flattened element to the result array
        $result[] = $flattenedElement;

        // If there are children, recursively flatten them
        if (!empty($element['children'])) {
            flattenArray($element['children'], $level + 1, $result);
        }
    }

    return $result;
}

// Your multidimensional array
$arr = [
    // ... your array structure ...
];

// Flatten the array
$flattenedArray = flattenArray($arr);

// Print the result
print_r($flattenedArray);


© www.soinside.com 2019 - 2024. All rights reserved.