php/doc-zh

ksort sorting is inconsistent

ClassmateLin opened this issue · 1 comments

From manual page: https://php.net/function.ksort


php7

$data = [ "user_id" => 1, "0"=>1 ]; 
ksort($data);
var_dump($data);

result:

array(2) {
  'user_id' =>int(1)
  [0] =>int(1)
}

php8:

$data = [ "user_id" => 1, "0"=>1 ]; 
ksort($data);
var_dump($data);

result:

array(2) {
  [0] =>int(1)
  'user_id' => int(1)
}

Note that ksort will NOT help you much if numeric and string keys are mixed together.

see https://3v4l.org/G6sF5

image