From: <ir...@ms...> - 2002-09-07 20:09:58
|
On Sat, Sep 07, 2002 at 06:52:37PM +0000, Clark C. Evans wrote: > I have a question about PHP. It says that it is an "ordered map" but > all of the examples I've seen show an associative container. In other > words, is array( 'a' => 'b', 'c' => 'd') distinguishable from > array( 'c' => 'd', 'a' => 'b'). If not, it is an associative container > and not a "ordered map" as it says. If it is an ordered map, what > is the syntax to get the nth key? I haven't looked at this specifically in a while, but from memory it's ordered because they keys stay in the order you define them. If you print_r the array or use the iteration functions, the keys come out in their original order. There are several functions to sort the array in various ways, and then the new order persists. In contrast, Python dictionaries do not preserve the order at all. If you print a dictionary or do dic.keys(), they come out in a random order. Sometimes this is frustrating because it means you have to keep a separate list around to show which order you want the elements processed in. Of course, there's no concept of sorting a dictionary in Python. You'd have to get the keys as a list and sort that. I assume your two arrays would compare equal. As for getting the nth value, $arr[5] gets the item with numeric subscript 5. If you mean the absolute fifth element, you could run the next() iterator function five times or... I don't know. What does foreach do? A C-like for loop would get only the numeric subscripts. -- -Mike (Iron) Orr, ir...@ms... (if mail problems: ms...@oz...) http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol |