I have been trying to convert my Multidimensional array to single-dimensional array. I have tried with PHP array in-built functions, but not able to get required output.
Problem
I have Multidimensional array for categories hierarchy, that stores child category in ‘children’ key. there is no limit for children level, it could be any number.
Result
I am trying to convert this Multidimensional array to single dimensional array, but children array should be added just after their parent with having extra key ‘level’ to display hierarchy order.
Multidimensional array
$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" => [],
],
],
],
],
],
];
Single dimensional array, I am trying to get
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
)
)
Here what I have tried,
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>";
2
Answers
you need to use recursion
You could do this by using a stack: