PHP 行为和数组指针

标签 php arrays pointers

我在阅读 PHP 手册(特别是 each() 函数)时遇到了以下警告:

Caution
Because assigning an array to another variable resets the original array's pointer, our example above would cause an endless loop had we assigned $fruit to another variable inside the loop.

还有一个例子:

<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

reset($fruit);
while (list($key, $val) = each($fruit)) {
    echo "$key => $val\n";
}
?>

好的。这说得通。但我决定做一个简单的测试:

<?php
    $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

    foreach ($fruit as $key => $name) {
        printf("[%s] => [%s]\n", $key, $name);
    }

    $fruit2 = $fruit;
    echo current($fruit);
?>

结果符合预期:指针已重置。我的问题是指针是否仅在数组结束后才重置?

例如:

<?php
    $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');

    foreach ($fruit as $key => $name) {
        printf("[%s] => [%s]\n", $key, $name);
    }

    reset($fruit);
    next($fruit)."\n";
    $fruit2 = $fruit;
    echo current($fruit);
?>

指针保留在第二个数组元素 ('b' => 'banana') 中。 这种行为是语言的特征?

谢谢你,抱歉英语不好。

最佳答案

This behavior is characteristic of language?

PHP 数组中“指针”的含义与“指针”的一般含义(在 C/C++ 或其他允许程序员直接访问内存的语言中)不同。

PHP 中没有指针。 array数据类型在其包含的值列表内部保留一个游标。它被称为数组的内部指针,它被函数修改 reset() , next() , prev() , end() , each() 也许还有其他人。它可用于像这样遍历数组:

$array = array(1, 2, 3);
while (list($key, $val) = each($array)) {
    echo($key.' => '.$val."\n");
}

没有可靠的方法使用 next() 迭代数组或 prev() 因为他们返回 FALSE当没有更多元素要迭代但它们也返回 FALSE 时当值 FALSE作为一个元素存储在数组中。

如果您只需要分析数组开头(或结尾)的几个项目,它们可能很有用。 F.e.假设我们有一个函数返回的整数数组,我们需要获取第一个不为零的值。

但是使用 foreach() 可以更轻松地实现这个目标:

$array = array(0, 0, 0, 2, 0, 1, 0, 3);
foreach ($array as $val) {
    if ($val != 0) {
        break;
    }
}
echo($val);           // prints "2"

array_shift() :

$array = array(0, 0, 0, 2, 0, 1, 0, 3);
do {
    $val = array_shift($array);
    if ($val != 0) {
        break;
    }
} while(count($array));
echo($val);           // prints "2"

The result is expected: the pointer has been reset. My question is if the pointer is reset only after the end of the array?

foreach() 的文档是错的。也许它在 PHP 3 和 PHP 4 上是正确的,但我认为自从引入 iterators 以来在 PHP 5 中 foreach() 的行为改变(变得更好)。

它说:

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.

As foreach relies on the internal array pointer, changing it within the loop may lead to unexpected behavior.

一个简单的测试与这个陈述相矛盾:

$array = array(1, 3, 5, 7, 9);

foreach ($array as $val1) {
    foreach ($array as $val2) {
        echo('$val1='.$val1.'; $val2='.$val2.'; ');
    }
    echo("\n");
}

它工作没有问题。如果 foreach() 它不应该工作正在使用内部数组指针。它可能会创建指针的副本。

你也可以尝试使用current() , next() , prev()reset()foreach()里面你会得到令人惊讶的,有时甚至是不一致的结果。

你最好使用foreach()遍历数组并且不以任何方式依赖内部指针。

函数reset()end()但是,当您需要获取数组的第一个和最后一个元素而不用担心键时,它们非常方便。

关于PHP 行为和数组指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30446546/

相关文章:

php - 基于浏览器的RPG : Using sessions to store player data

php - Symfony:不推荐使用 form_enctype

php - 如何使用数据库中的值创建 PHP 两列表?

c++ - 如何在链表的节点之间移动?

c - 使用 getline 将字符串读取到指针数组中 (C)

php - 如何在查询生成器中使用 unix_timestamp

集体或个人循环以获得最佳性能?

C Struct & Strcpy,点运算符是否解析为指针

javascript - 中国食物[array[0]] = array[array.length-1];

c - 字符数组的打印地址