Hacker News new | ask | show | jobs
by mccutchen 5541 days ago
> PHP has its own beauty, such as arrays which can support numeric and string indexes at the same time.

What's the difference between PHP's arrays and hashtables/dictionaries/etc in languages like Python, Ruby, and JavaScript?

As a Python programmer (who started with PHP years ago), PHP's arrays now seem sort of muddled or confusing. I prefer Python's dedicated list/dict types (though nothing is stopping you from mixing numeric and string keys in a dict).

1 comments

Don't get me wrong, I like Python's data types very much. And it's certainly nice to have the ability to have more low-level control over how your array is stored.

I mean beauty in the sense of appreciating something. Just because your wife isn't a supermodel, doesn't mean you cannot find her beautiful and have a good marriage. Once you accept PHP for what it is, you find that there's a certain file-oriented style of coding that is perfectly suited to it, that embraces the unique type of PHP arrays, and that autoloads files when needed.

In PHP, you have an array that is basically associative, BUT which you can iterate in the same order that keys are added. This data structure is very unique and often lets you write less code, since you don't have to store the order separately. It also happens to map to JSON very well. Javascript has something like this too, except there is no guarantee that all browsers will implement properties in the same order, and many people on the internet complain about this sort of thing:

http://code.google.com/p/chromium/issues/detail?id=883

http://code.google.com/p/chromium/issues/detail?id=20144

I have to say, I feel for those guys.

PHP "arrays" are not so unique.

http://ruby-doc.org/core/classes/Hash.html

"Hashes enumerate their values in the order that the corresponding keys were inserted."

Cool to know! Looks like ruby has 'em too :)
See also Python's recently added odict