How to convert an array to PHP [nesting]

Please, I need some help. How to convert my array through PHP

Array
(
    [0] => Apple
    [1] => Orange
    [2] => Tomato
)

For this

Array
(
    [Apple] => Array
        (
            [Orange] => Array
                (
                    [Tomato] => Array()
                )

        )

)

And I do not know how many elements are in my array. Thanks to everyone.

+4
source share
6 answers

Exit

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>";
+5
source

Try the following:

$array = array('apple','orange','tomato');
$count = count($array) - 1;
$tempArray = array();
for($i = $count; $i >= 0; $i--)
{
    $tempArray = array($array[$i] => $tempArray);
}
+5
source

:

$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);

:

Array
(
    [apple] => Array
        (
            [orange] => Array
                (
                    [tomato] => Array
                        (
                        )

                )

        )

)

1:

/ . , , , . , $target. .

$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);

:

Array
(
    [apple] => 
)
-----------------
Array
(
    [apple] => Array
        (
            [orange] => 
        )

)
-----------------
Array
(
    [apple] => Array
        (
            [orange] => Array
                (
                    [tomato] => 
                )

        )

)
-----------------
final array
Array
(
    [apple] => Array
        (
            [orange] => Array
                (
                    [tomato] => Array
                        (
                        )

                )

        )

)

$target foreach

+5

foreach 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>";

?>

[ ]

Array
(
    [Apple] => Array
        (
            [Orange] => Array
                (
                    [Tomato] => Array
                        (
                            [Banana] => Array
                                (
                                    [Papaya] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
+2

array_pop:

$fruits = [
    "Apple",
    "Orange",
    "Tomato",
    "Banana",
    "Papaya"
];

$output = [];
while ( $fruit = array_pop($fruits) )
{
    $output = [$fruit => $output];
}
+2
$result = array_reduce(array_reverse($fruits), function (array $acc, $fruit) {
    return [$fruit => $acc];
}, []);

$key => $value.

+1

Source: https://habr.com/ru/post/1621221/


All Articles