user01232 user01232 - 12 days ago 6
PHP Question

i'm getting the weird output in php array_reverse() functions

I'm getting the weird output

<?php
$a = array('1000'=>1,2,3,4,'1001'=>5);
var_dump(array_reverse($a));
?>


and I'm getting the output like this:

array (size=4)
0 => int 4
1 => int 3
2 => int 5
3 => int 1


value 2 is missing. Can anyone explain the code?

Answer
$a = array('1000'=>1,2,3,4,'1001'=>5);

means

create key entry 1000 with value 1
create key entry 1000+1 with value 2
create key entry 1000+2 with value 3
create key entry 1000+3 with value 4

then

create key entry 1001 with value 5

which already exists (with value 2), so is overwritten with the new value

Comments