Are there dictionaries in php? Are there dictionaries in php? php php

Are there dictionaries in php?


http://php.net/manual/en/language.types.array.php

<?php$array = array(    "foo" => "bar",    "bar" => "foo",);// as of PHP 5.4$array = [    "foo" => "bar",    "bar" => "foo",];?>

Standard arrays can be used that way.


No, there are no dictionaries in php. The closest thing you have is an array. However, an array is different than a dictionary in that arrays have both an index and a key. Dictionaries only have keys and no index. What do I mean by that?

$array = array(    "foo" => "bar",    "bar" => "foo");// as of PHP 5.4$array = [    "foo" => "bar",    "bar" => "foo",];

The following line is allowed with the above array but would give an error if it was a dictionary.

print $array[0]

Python has both arrays and dictionaries.


Normal array can serve as a dictionary data structure. In general it has multipurpose usage: array, list (vector), hash table, dictionary, collection, stack, queue etc.

$names = [    'bob' => 27,    'billy' => 43,    'sam' => 76,];$names['bob'];

And because of wide design it gains no full benefits of specific data structure. You can implement your own dictionary by extending an ArrayObject or you can use SplObjectStorage class which is map (dictionary) implementation allowing objects to be assigned as keys.