PHP 每个循环使数组更深一层

发布于 2024-12-11 09:31:52 字数 619 浏览 0 评论 0原文

我试图循环遍历一个数组,每次都向另一个数组添加一个新级别。让我说明一下 - 变量 $arr 的值每次都不同

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

Looping

$index[$arr[0]];

Looping

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

Looping

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

--循环遍历所有 $arr 的项目,完成,结果是 $index["1"]["5"]["6"]--

问题是我不知道 $arr 数组包含多少值。然后,当 $arr 的第一个值循环到下一个数组时,我不知道如何从 $index["1"] 继续级别(换句话说:添加另一个键)..

有人吗?

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

Looping

$index[$arr[0]];

Looping

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

Looping

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

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

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)..

Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-12-18 09:31:52

您可以在此处使用引用:

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

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

outputs

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

要使用其他值覆盖最后一个元素,您只需

$c = 'blubber';

在循环后面添加行:,因为 $c 是对最深数组级别的引用,当循环时完成了。

You can use references here:

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

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

outputs

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

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

$c = 'blubber';

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

月下客 2024-12-18 09:31:52
function add_inner_array(&$array, $index) {
    if(isset($array[$index])) return true;
    else {
        $array[$index] = array();
        return true;
    }
}

$a = array(1,5,6);
$index = array();
$pass =& $index;
foreach($a as $k) {
    add_inner_array($pass, $k);
    $pass =& $pass[$k];
}
function add_inner_array(&$array, $index) {
    if(isset($array[$index])) return true;
    else {
        $array[$index] = array();
        return true;
    }
}

$a = array(1,5,6);
$index = array();
$pass =& $index;
foreach($a as $k) {
    add_inner_array($pass, $k);
    $pass =& $pass[$k];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文