Hacker News new | past | comments | ask | show | jobs | submit login

A single downvote on a subreddit is indicative of the PHP community as a whole? Well, shit, time to ditch the language...

Why would you think the third value in ['ab', 'cd', '10', 'gh', '24'] would have a key of '10'? That's the value, not the key.

The docs are quite clear on how this works:

http://php.net/manual/en/control-structures.foreach.php

foreach (array_expression as $value)

vs.

foreach (array_expression as $key => $value)




>Why would you think the third value in ['ab', 'cd', '10', 'gh', '24'] would have a key of '10'? That's the value, not the key.

Can you please take one more look? Values in the first array is used as keys to set values in the second array..


You've misunderstood. He only uses that (flat) array to populate the key-value array $c. His problem is that PHP oh-so-helpfully converts the string value to an int value. It's actually impossible to have string values as indices of an array when they are parsable as numbers in PHP. One of the many foibles that stem from conflating flat arrays and key-value maps. (For a real fun time, try reading the docs for array_merge.)

Anyway, it's not surprising. /r/PHP doesn't handle criticism well.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: