PHP:使用非字母和非数字键对数组进行排序

发布于 2024-10-31 10:10:31 字数 485 浏览 1 评论 0原文

我正在尝试使用 PHP 的 ksort 来对这个数组进行排序:

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

但是包含 {attr}... 的键没有被排序,它保留在同一个位置,而其他键被排序。对该数组进行排序的最佳方法是什么?

I'm trying to use PHP's ksort to sort this array:

Array(
    [district_name] => District name
    [email] => [email protected]
    [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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜声 2024-11-07 10:10:31

我无法证实这一点。此代码按预期排序(“{attr}id”位于结果数组中的最后):

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

ksort($arr);

var_dump($arr);

请确保您的源数组没问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文