Does PHP have an ordered dictionary?

Does PHP have an ordered dictionary like in Python ? IE, each pair of key values ​​additionally has an associated serial number.

+8
php data-structures
source share
3 answers

The way PHP arrays work out of the box. Each key / value pair has a serial number, so the insertion order is remembered. You can easily test it yourself:

http://ideone.com/sXfeI

+5
source share

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 ​​with keys. This type is optimized for several different applications; it can be considered as an array, list (vector), hash table (map implementation), dictionary, collection, stack, queue, and possibly more. Other arrays can be used as array values, trees and multidimensional arrays are also possible.

PHP Array Documents

+3
source share

PHP arrays work 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 
+2
source share

All Articles