Php array crash issue

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 should work, is it? the values ​​must be sorted and the keys stored, while the opposite happens - just like ksort. What is the problem?

Please help me.

thank

+3
source share
3 answers

Works well for me: http://codepad.org/o6pZ8ess

result:

array(4) {
  [1]=>
  int(1)
  [4]=>
  int(3)
  [7]=>
  int(8)
  [2]=>
  int(9)
}
+4
source

Works great for me, you tried:

asort($myarr, SORT_NUMERIC);
+3
source

I believe this is not possible and the accepted answer is incorrect. I do not believe that you can have whole keys in non-digital order.

0
source

Source: https://habr.com/ru/post/1772634/


All Articles