4

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.

mickmackusa
49.3k13 gold badges98 silver badges165 bronze badges
asked Dec 22, 2015 at 6:58
0

6 Answers 6

5

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>";
answered Dec 22, 2015 at 7:18
Sign up to request clarification or add additional context in comments.

Comments

5

Try This:

$array = array('apple','orange','tomato');
$count = count($array) - 1;
$tempArray = array();
for($i = $count; $i >= 0; $i--)
{
 $tempArray = array($array[$i] => $tempArray);
}
answered Dec 22, 2015 at 7:01

Comments

5

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

answered Dec 22, 2015 at 7:17

Comments

2

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
 (
 )
 )
 )
 )
 )
)
answered Dec 22, 2015 at 7:51

Comments

2

While loop and array_pop:

$fruits = [
 "Apple",
 "Orange",
 "Tomato",
 "Banana",
 "Papaya"
];
$output = [];
while ( $fruit = array_pop($fruits) )
{
 $output = [$fruit => $output];
}
answered Dec 22, 2015 at 9:24

Comments

1
$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.

answered Dec 22, 2015 at 9:33

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.