如何在PHP中转换数组[嵌套][重复]

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

拜托,我需要帮助。 如何通过 PHP 转换我的数组

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

对此

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

        )

)

我不知道我的数组中有多少元素。 谢谢大家。

php arrays
6个回答
5
投票

输出

Array
(
    [0] => Apple
    [1] => Orange
    [2] => Tomato
    [3] => Banana
    [4] => Papaya
)
Array
(
    [Apple] => Array
        (
            [Orange] => Array
                (
                    [Tomato] => Array
                        (
                            [Banana] => Array
                                (
                                    [Papaya] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

代码

$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
投票

试试这个:

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

5
投票

尝试一下:

$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
的值


2
投票

您也可以使用

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

?>

[概念证明]

数组
(
    [苹果] => 数组
        (
            [橙色] => 数组
                (
                    [番茄] => 数组
                        (
                            [香蕉] => 数组
                                (
                                    [木瓜] => 数组
                                        (
                                        )

                                )

                        )

                )

        )

)

2
投票

While 循环和array_pop:

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

$output = [];
while ( $fruit = array_pop($fruits) )
{
    $output = [$fruit => $output];
}

1
投票
$result = array_reduce(array_reverse($fruits), function (array $acc, $fruit) {
    return [$fruit => $acc];
}, []);

您只需从内到外开始并将值包装到一个

$key => $value
数组中即可。

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