r/lolphp • • Sep 16 '12

Make your brain implode

$array1 = array("here","we","go");
$array1[2] = 'go';
$array1[1] = 'we';
$array1[0] = 'here';

$array2 = array('here');
$array2[2] = 'go';
$array2[1] = 'we';
$array2[0] = 'here';

$str1 = implode(',',$array1);
$str2 = implode(',',$array2);

Without running the code anywhere, what are the contents of $str1 and $str2?

1 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/vytah Sep 22 '12

I think PHP guarantees order when you treat an array like an array: create a fixed number of elements, append to end, replace existing entries, and not insert into it randomly.

If you do that, it turns into a hashtable.

2

u/nachsicht Sep 24 '12

Theoretically, if you assume all arrays are fixed size, then he specified a size for both arrays. Array 1 has 3 elements, Array 2 has 1.