Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php array asort problem

Tags:

arrays

php

this is my array:

$myarr = array(
               4 => 3,
               2 => 9,
               7 => 8,
               1 => 1
              );

when i do :

asort($myarr);

$myarr becomes:

array( 
      1 => 1,
      2 => 9,
      4 => 3,
      7 => 8
     );

This is not how it is supposed to work,right? the values should be sorted and keys maintained, while the reverse is happening - just like ksort. What can the problem be?

Please help me out.

Thanks

like image 377
Prashant Avatar asked Jan 22 '23 04:01

Prashant


2 Answers

Works fine to me : http://codepad.org/o6pZ8ess

result :

array(4) {
  [1]=>
  int(1)
  [4]=>
  int(3)
  [7]=>
  int(8)
  [2]=>
  int(9)
}
like image 62
Shikiryu Avatar answered Jan 29 '23 13:01

Shikiryu


Works fine for me, did you try:

asort($myarr, SORT_NUMERIC);
like image 42
Jochen Avatar answered Jan 29 '23 13:01

Jochen