PHP-Arrays: Maps and lists

at 2009-04-05 in EditorialFurther readingRFCs by friebe (0 comments)

In the PHP world, arrays are maps are lists:

<?php 
$a= array(1, 2, 3); // #1: A list
$b= array('key' => 'value', ...); // #2: A map
$c= array(1, 2, 'a' => 'b'); // #3: Mix of both
?>

Now this is (almost) perfect as long as you stay inside the PHP world; the only thing you have to know is when to be able to use array functions operating on "associative arrays" (like asort) instead of "numeric arrays" (sort), which is usually achieved by a bit of discipline. This is where a slight problem even inside the PHP world (and that's why it's only almost perfect) starts showing: There is no easy way to keep the both apart.


We've been there quite a while ago, see the following blog entries from 2005:


Now the problem starts becoming more apparent when you start communicating with other programming languages. Nearly any other of them has separate concepts for maps and lists:

Java
An array is an immutable, zero-based list of values of one (super-)type. Maps are not supported syntactically but by means of java.util.Map implementations.

C#
Same as in Java, although arrays are objects (of class System.Array). Maps are supported via the System.Collections.IDictionary implementations.

Groovy
Groovy has native language support lists and maps - see here and here.
  def list = [5, 6, 7, 8]
def map = [name:"Gromit", likes:"cheese", id:1234]

Ruby
In Ruby, everything is an object, and so are arrays and hashes:
  list = [ 1, 2, 3 ]
map = { "one" => 2, "two" => "2"}

Python
Python knows lists and dicts with a syntax quite similar to that of Ruby.
  list = [ 1, 2, 3 ]
map = { "one" : 1, "two" : 2 }
There also is an array type that has the same (super-)type restrictions as Java arrays for example do.


For more on this topic, see also the Wikipedia entries "Associative array" and "Array".


Summing it up
As can be seen, the following would motivate having two different types for lists and maps:

  • Interopability - The only other programming language allowing something similar to what PHP does is JavaScript, see here. As soon as we start talking to other languages via SOAP, XMLRPC, EASC or any other protocol, we need to be able to distinguish.
  • Readability - list should be dumped as 1, 2, 3 while a map's representation would need to contain key information.
  • Memory efficiency - Basically, lists can be represented internally in a more memory-efficient way.
  • Type restrictions and safety - What if we pass an associative array to a PHP function that internally uses a for ($i= 0; $i < sizeof($a); $i++) { }-style loop to iterate? Right: It's not going to work.

This is why we have created RFC #0184.



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES

Related

Find related articles by a search for «PHP-Arrays:».