如何覆盖php中的指针关联?或者如何操作n维数组?

发布于 2024-09-24 14:18:14 字数 355 浏览 0 评论 0原文

我在 php (&) 中的指针操作符上遇到了一个令人讨厌的小问题。 我想循环一个 while 循环,它将数据写入数组中。 每次它都应该写入数组的下一个维度(第一个维度是 $array,然后是 $array[0],然后是 array[0][0],等等)。 我想通过使用指针链接到 $array 来实现此目的,然后像这样更改指针:

$pointer = &array;
while($bla){
  $pointer = &$pointer[0];
}

因此,每次触发 while 时,指针都会链接到 $array 的另一个维度。 但这似乎不起作用......

我真的很感谢你的帮助,谢谢。

I have a nasty little problem with the pointer opperator in php (&).
I want to loop through a while loop, which writes data in an array.
Each time it is supposed to write into the next dimension of the array (1st in $array, then in $array[0], then in array[0][0], etc).
I wanted to do this by linking to $array with a pointer, then changing the pointer like this:

$pointer = &array;
while($bla){
  $pointer = &$pointer[0];
}

So everytime while is triggered the pointer links to a further dimension of $array.
That doesn't seem to work though...

I would really appretiate your help, thank you.

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

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

发布评论

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

评论(2

逆夏时光 2024-10-01 14:18:14

我尝试了你的代码并且它有效。测试:

<?php
    error_reporting(E_ALL | E_STRICT);

    $array = array();
    $ptr =& $array;
    for ($i = 0; $i < 10; ++$i) {
        $ptr[0] = array();
        $ptr =& $ptr[0];
    }

    unset($ptr);
    var_dump($array);

    $ptr =& $array;
    while (!empty($ptr)){
        $ptr =& $ptr[0];
        var_dump($ptr);
    }

首先创建数组,然后执行循环。

I tried your code and it works. Test:

<?php
    error_reporting(E_ALL | E_STRICT);

    $array = array();
    $ptr =& $array;
    for ($i = 0; $i < 10; ++$i) {
        $ptr[0] = array();
        $ptr =& $ptr[0];
    }

    unset($ptr);
    var_dump($array);

    $ptr =& $array;
    while (!empty($ptr)){
        $ptr =& $ptr[0];
        var_dump($ptr);
    }

This first creates the array and then does the loop.

拧巴小姐 2024-10-01 14:18:14

我不确定这是否是您想要的,但您在这里。 :-)

$a = array();

$b = &$a;

for ($i = 0; $i < 6; $i++) {
    $a[0] = array();
    $a = &$a[0];
}

print_r($b);

输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

I'm not sure if this is what you wanted, but here you are. :-)

$a = array();

$b = &$a;

for ($i = 0; $i < 6; $i++) {
    $a[0] = array();
    $a = &$a[0];
}

print_r($b);

Outputs:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [0] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文