PHP更深一层的阵列中的每个循环发 [英] PHP One level deeper in array each loop made

查看:129
本文介绍了PHP更深一层的阵列中的每个循环发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个数组试图循环,增加每一次一个新的水平,以另一个数组。让我说明 - 变量$改编的价值观是不同的,每次

I'm trying to loop through one array, adding a new level to another array each time. Let me illustrate - variable $arr's values are different each time

$arr = array("1","5","6");

循环

$index[$arr[0]];

循环

$index["1"][$arr[1]]  // "1" since this key was filled in by the previous loop, continuing with a new key

循环

$index["1"]["5"][$arr[2]] // same as previous loop

- 循环对所有$改编的项目,做的,结果是$指数[1] [5] [6] -

--looped over all $arr's items, done, result is $index["1"]["5"]["6"]--

问题是我不知道的 $改编阵列多少值包含的内容。然后,我不知道如何从,例如, $指数[1] 时的第一个值 $改编一直循环到下一个阵列级别(换句话说,添加另一个键)。

The problem is I won't know how much values the $arr array contains. Then, I don't know how to continue from, for example, $index["1"] when the first value of $arr has been looped to the next array level (other words: add another key)..

有人吗?

推荐答案

您可以在这里使用引用:

You can use references here:

$a = array("1","5","6");
$b = array();
$c =& $b;

foreach ($a as $k) {
    $c[$k] = array();
    $c     =& $c[$k];
}

输出

Array 
    (
    [1] => Array
        (
            [5] => Array
                (
                    [6] => Array
                        (
                        )
                )
        )
)

要覆盖与其他一些价值的最后一个元素,可以只添加一行:

To overwrite the last element with some other value, you can just add the line:

$c = 'blubber';

在循环后,因为的 $ C 的是最深的阵列级别,当循环结束的参考。

after the loop, because $c is a reference to the deepest array level, when the loop is finished.

这篇关于PHP更深一层的阵列中的每个循环发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆