什么是PHP中的数组内部指针? [英] What is an array internal pointer in PHP?

查看:147
本文介绍了什么是PHP中的数组内部指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用端()来设置一个数组最后一个单元的内部指针。然后我使用键()来得到最后一个元素的关键。

I am using end() to set the internal pointer of an array to its last element. Then I am using key() to get the key of that last element.

例如:

$array = ('one' => 'fish', 'two' => 'fish', 'red' => 'fish', 'blue' => 'fish');
end($array)
$last_key = key($array);

这是我不明白的唯一的事情是什么一个数组的内部指针指向的的完全吻合。有人可以解释给我吗?我一直很努力,但无法找到一个解释。

The only thing that I do not understand is what the internal pointer of an array is exactly. Can someone explain it to me? I've been trying but cannot find an explanation.

另外,如何设置一个数组的内部指针影响阵列?

Also, how does setting the internal pointer of an array affect that array?

推荐答案

有对PHP中阵列,幕后内部实现,用C写的这个实现定义的数组数据的实际存储在细节内存阵列的行为,他们怎么可以访问等等。这个C实现的部分是一个数组指针,它只是指向数组的特定索引。在非常简单的PHP code,它是这样的:

There's an internal implementation for "arrays" in PHP "behind the scenes", written in C. This implementation defines the details of how array data is actually stored in memory, how arrays behave, how they can be accessed etc. Part of this C implementation is an "array pointer", which simply points to a specific index of the array. In very simplified PHP code, it's something like this:

class Array {

    private $data = [];
    private $pointer = 0;

    public function key() {
        return $this->data[$this->pointer]['key'];
    }

}

您不必从PHP code数组指针直接访问,你可以修改和间接使用像结束,<$ C PHP函数影响它的$ C>重置,每个等,这是必要的,可以使这些职能的工作;否则你无法使用循环数组的next(),因为它在那里会记得下一步进入的是什么?

You do not have direct access to this array pointer from PHP code, you can just modify and influence it indirectly using PHP functions like end, reset, each etc. It is necessary for making those functions work; otherwise you couldn't iterate an array using next(), because where would it remember what the "next" entry is?

这篇关于什么是PHP中的数组内部指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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