问题描述
我来自蟒蛇背景和蟒蛇数据类型类似于(字典)是一个无序设置键值对。
I come from python background and the python datatype which is similar (a dictionary) is an unordered set of key value pairs.
我想知道如果PHP关联数组是无序的?他们似乎是有序的。
I am wondering if PHP associative arrays are unordered? They appear to be ordered.
$test = array(
'test' => 'test',
'bar' => 'bar',
);
var_dump($test);
var_dump(array_slice($test, 0, 1));
测试永远是酒吧前,你看到我可以切片这个数组。因此,这始终保证整个PHP版本的订购?是为了以防我已经宣布与阵列的顺序?因此,一些在内部指向测试[0]放置在数组中?我已阅读http://php.net/manual/en/language.types.array.php但它并没有在这个问题上流下了太多的光。我AP preciate你的反应。泰
Test always comes before bar and I can slice this array as you see. So is this always guaranteed to be ordered across php versions? Is the order just the order that I have declared the array with? So something is internally pointing 'test' to place [0] in the array? I have read http://php.net/manual/en/language.types.array.php but it doesn't shed too much light on this issue. I appreciate your responses. Ty
推荐答案
PHP关联数组(以及数字阵列)的排序的,和PHP提供的各种功能来处理数组键排序像,的和的
PHP associative arrays (as well as numeric arrays) are ordered, and PHP supplies various functions to deal with the array key ordering like ksort()
, uksort()
, and krsort()
此外,PHP允许你声明与数字键阵列失灵:
Further, PHP allows you to declare arrays with numeric keys out of order:
$a = array(3 => 'three', 1 => 'one', 2 => 'two');
print_r($a);
Array
(
[3] => three
[1] => one
[2] => two
)
// Sort into numeric order
ksort($a);
print_r($a);
Array
(
[1] => one
[2] => two
[3] => three
)
PHP中的数组实际上是一个有序图。图是一种把值keys的类型。这种类型在很多方面做了优化;它可以被视为一个阵列,列表(矢量),哈希表(映射的实现),字典,集合,栈,队列和可能更多。作为数组值可以是其他数组,树和多维数组也是可能的。
这篇关于是PHP关联数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!