是PHP关联数组排序? [英] Are PHP Associative Arrays ordered?

查看:139
本文介绍了是PHP关联数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自蟒蛇背景和蟒蛇数据类型类似于(字典)是一个无序设置键值对。

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]放置在数组中?我已阅读<一个href=\"http://php.net/manual/en/language.types.array.php\">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提供的各种功能来处理数组键排序像 ksort() ,的 uksort() 和的 krsort()

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的类型。这种类型在很多方面做了优化;它可以被视为一个阵列,列表(矢量),哈希表(映射的实现),字典,集合,栈,队列和可能更多。作为数组值可以是其他数组,树和多维数组也是可能的。

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

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

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