How to convert my array:
Array
(
[0] => Apple
[1] => Orange
[2] => Tomato
)
To this:
Array
(
[Apple] => Array
(
[Orange] => Array
(
[Tomato] => Array()
)
)
)
I do not know how many elements will be in my array.
6 Answers 6
Output
Array
(
[0] => Apple
[1] => Orange
[2] => Tomato
[3] => Banana
[4] => Papaya
)
Array
(
[Apple] => Array
(
[Orange] => Array
(
[Tomato] => Array
(
[Banana] => Array
(
[Papaya] => Array
(
)
)
)
)
)
)
Code
$fruits = [
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
];
// Result Array
$result = [
$fruits[count($fruits) - 1] => []
];
// Process
for ($counter = count($fruits) - 2; $counter >= 0; $counter--) {
$temp = $result;
unset($result);
$result[$fruits[$counter]] = $temp;
}
// Display
echo "<pre>".print_r($fruits, true)."</pre>";
echo "<pre>".print_r($result, true)."</pre>";
Comments
Try This:
$array = array('apple','orange','tomato');
$count = count($array) - 1;
$tempArray = array();
for($i = $count; $i >= 0; $i--)
{
$tempArray = array($array[$i] => $tempArray);
}
Comments
try it with:
$target = array();
$value = array();
$path = array('apple', 'orange', 'tomato');
$rv = &$target;
foreach($path as $pk)
{
$rv = &$rv[$pk];
}
$rv = $value;
unset($rv);
print_r($target);
output:
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] => Array
(
)
)
)
)
Update 1: Explaination
Here I am using reference/variable alias to traverse the dynamic stack of keys. The reference makes it possible to use a stack instead of recursion which is generally more lean. Additionally this code prevents to overwrite existing elements in the $target array.
For more detail on references have a look at Reference Explained
$target = array(); //target array where we will store required out put
$value = array(); //last value i.e. blank array
$path = array('apple', 'orange', 'tomato'); //current array
$rv = &$target; //assign address of $target to $rv (reference variable)
foreach($path as $pk)
{
$rv = &$rv[$pk]; // Unused reference [ex. $rv['apple'] then $rv['apple']['orange'] .. so on ] - actually assigned to $target by reference
print_r($target);
echo '-----------------<br />';
}
$rv = $value; //here $rv have unused refernce of value tomato so finally assigned a blank array to key tomoto
//
unset($rv); // Array copy is now unaffected by above reference
echo "final array<br />";
print_r($target);
output:
Array
(
[apple] =>
)
-----------------
Array
(
[apple] => Array
(
[orange] =>
)
)
-----------------
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] =>
)
)
)
-----------------
final array
Array
(
[apple] => Array
(
[orange] => Array
(
[tomato] => Array
(
)
)
)
)
In output of explaination you can trace the value of $target in foreach loop
Comments
You can try this way also with foreach and ksort:
<?php
$fruits = array(
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
);
krsort($fruits);
$tmp = array();
foreach($fruits as $fruit){
$tmp = array($fruit => $tmp);
}
echo "<pre>".print_r($tmp, true)."</pre>";
?>
[Proof of concept]
Array ( [Apple] => Array ( [Orange] => Array ( [Tomato] => Array ( [Banana] => Array ( [Papaya] => Array ( ) ) ) ) ) )
Comments
While loop and array_pop:
$fruits = [
"Apple",
"Orange",
"Tomato",
"Banana",
"Papaya"
];
$output = [];
while ( $fruit = array_pop($fruits) )
{
$output = [$fruit => $output];
}
Comments
$result = array_reduce(array_reverse($fruits), function (array $acc, $fruit) {
return [$fruit => $acc];
}, []);
You simply start from the inside out and wrap the value into a $key => $value array.