开发者

PHP: sorting array with non-alphabetic and non-numeric keys

开发者 https://www.devze.com 2023-02-23 22:10 出处:网络
I\'m trying to us开发者_JAVA百科e PHP\'s ksort to sort this array: Array( [district_name] => District name

I'm trying to us开发者_JAVA百科e PHP's ksort to sort this array:

Array(
    [district_name] => District name
    [email] => email@email.com
    [name] => Name of item
    [number] => 191
    [phone] => +41234568789
    [{attr}id] => 2
    [questions] => Array(...)
)

But the key that contains {attr}... does not get sorted, it stays at the same place while the other keys gets sorted. What is the best method to sort this array?


I can't confirm this. This code sorts as expected ("{attr}id" is last in the resulting array):

$arr = array(
  "district_name" => "foo",
  "email" => "foo",
  "name" => "foo",
  "number" => "foo",
  "phone" => "foo",
  '{attr}id' => "foo",
  "questions" => "foo",
);

ksort($arr);

var_dump($arr);

Please make sure your source array is okay.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号