Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are PHP Associative Arrays ordered?

I come from python background and the python datatype which is similar (a dictionary) is an unordered set of key value pairs.

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)); 

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

like image 886
dm03514 Avatar asked Jun 06 '12 13:06

dm03514


People also ask

Does PHP preserve array order?

PHP array is an ordered map, so, it's a map that keeps the order. array elements just keep the order since they were added that's all.

How do you sort an associative array in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

What are the advantages of associative arrays in PHP?

Advantages of Associative ArrayWe can save more data, as we can have a string as key to the array element, where we can have associated data to the value to be stored, like in our example, we stored the type of the car as key along with the name of the car as value.

What is meant by associative array in PHP?

Associative Array - It refers to an array with strings as an index. Rather than storing element values in a strict linear index order, this stores them in combination with key values. Multiple indices are used to access values in a multidimensional array, which contains one or more arrays.


2 Answers

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()

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 ) 

From the documentation:

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.

like image 72
Michael Berkowski Avatar answered Oct 13 '22 22:10

Michael Berkowski


The documentation states:

An array in PHP is actually an ordered map. 

So yes, they are always ordered. Arrays are implemented as a hash table.

like image 24
alexn Avatar answered Oct 13 '22 22:10

alexn