开发者

Does PHP have an ordered dictionary?

开发者 https://www.devze.com 2023-04-06 19:58 出处:网络
Does PHP have an Ordered D开发者_运维百科ictionary, like that in Python? IE, each key value pair additionally has an ordinal associated with it.That\'s how PHP arrays work out of the box. Each key/val

Does PHP have an Ordered D开发者_运维百科ictionary, like that in Python? IE, each key value pair additionally has an ordinal associated with it.


That's how PHP arrays work out of the box. Each key/value pair has an ordinal number, so the insertion order is remembered. You can easily test it yourself:

http://ideone.com/sXfeI


If I understand the description in the python docs correctly, then yes. PHP Arrays are actually only ordered maps:

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 Array docs


PHP arrays work this way by default.

$arr = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => 4);
var_dump($arr); // 1, 2, 3, 4

unset($arr['three']);
var_dump($arr); // 1, 2, 4

$arr['five'] = 5;
var_dump($arr); // 1, 2, 4, 5
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号