PHP中数组的负索引

时间:2021-03-01 22:32:57

I found some code that uses negative array indices. Then, I try to use it, nothing special happens. It behaves normally. I can retrieve all elements by using a standard foreach loop.

我找到了一些使用负数组索引的代码。然后,我尝试使用它,没有什么特别的事情发生。它表现正常。我可以使用标准的foreach循环检索所有元素。

So, what is the purpose to use those negative indices? And when should I use it?

那么,使用这些负面指数的目的是什么?我什么时候应该使用它?

4 个解决方案

#1


22  

An array, in PHP, is actually just some kind of an ordered map : you can use integers (positive or negative), but also strings, as keys -- and there will not be much of difference.

PHP中的数组实际上只是某种有序映射:您可以使用整数(正数或负数),也可以使用字符串作为键 - 并且不会有太大区别。

#2


13  

Negative array keys have no special meaning in PHP, as (like any other value) they can be the keys of an associative array.

负数组键在PHP中没有特殊含义,因为(与任何其他值一样)它们可以是关联数组的键。

$arr = array(-1 => 5);
echo $arr[-1];

Some of PHP's standard library functions (the ones that expect regular arrays with only natural integer indices), however, take negative offsets to mean "count from the end instead of the beginning". array_slice is one such example.

然而,PHP的一些标准库函数(期望具有自然整数索引的常规数组的函数)采用负偏移来表示“从结束而非开始计数”。 array_slice就是这样一个例子。

#3


6  

Negative array indexes don't have a special meaning (i.e. get the last/second last element etc.) in PHP. To get the last element of an array use:

负数组索引在PHP中没有特殊含义(即获取最后/第二个最后一个元素等)。要获取数组的最后一个元素,请使用:

$last = end($array);

To get the second last add:

要获得第二个最后添加:

$secondLast = prev($array);

Keep in mind that these functions modify the arrays internal pointer. To reset it use:

请记住,这些函数会修改数组内部指针。要重置它,请使用:

reset($array);

#4


6  

From 7.1 onward, we have an important and practical special case, i.e. when using the array syntax to access particular characters of a string from backwards:

从7.1开始,我们有一个重要而实用的特例,即使用数组语法从后向访问字符串的特定字符时:

$str = "123";
$empty = "";

echo "LAST CHAR of str == '$str[-1]'<br>"; // '3'
echo "LAST CHAR of empty == '$empty[-1]'<br>"; // '', Notice: Uninitialized string offset: -1

#1


22  

An array, in PHP, is actually just some kind of an ordered map : you can use integers (positive or negative), but also strings, as keys -- and there will not be much of difference.

PHP中的数组实际上只是某种有序映射:您可以使用整数(正数或负数),也可以使用字符串作为键 - 并且不会有太大区别。

#2


13  

Negative array keys have no special meaning in PHP, as (like any other value) they can be the keys of an associative array.

负数组键在PHP中没有特殊含义,因为(与任何其他值一样)它们可以是关联数组的键。

$arr = array(-1 => 5);
echo $arr[-1];

Some of PHP's standard library functions (the ones that expect regular arrays with only natural integer indices), however, take negative offsets to mean "count from the end instead of the beginning". array_slice is one such example.

然而,PHP的一些标准库函数(期望具有自然整数索引的常规数组的函数)采用负偏移来表示“从结束而非开始计数”。 array_slice就是这样一个例子。

#3


6  

Negative array indexes don't have a special meaning (i.e. get the last/second last element etc.) in PHP. To get the last element of an array use:

负数组索引在PHP中没有特殊含义(即获取最后/第二个最后一个元素等)。要获取数组的最后一个元素,请使用:

$last = end($array);

To get the second last add:

要获得第二个最后添加:

$secondLast = prev($array);

Keep in mind that these functions modify the arrays internal pointer. To reset it use:

请记住,这些函数会修改数组内部指针。要重置它,请使用:

reset($array);

#4


6  

From 7.1 onward, we have an important and practical special case, i.e. when using the array syntax to access particular characters of a string from backwards:

从7.1开始,我们有一个重要而实用的特例,即使用数组语法从后向访问字符串的特定字符时:

$str = "123";
$empty = "";

echo "LAST CHAR of str == '$str[-1]'<br>"; // '3'
echo "LAST CHAR of empty == '$empty[-1]'<br>"; // '', Notice: Uninitialized string offset: -1