如何在多维数组中按键排序

发布于 2024-11-26 23:36:33 字数 768 浏览 0 评论 0原文

我制作了这个虚构的会员名单。我想在每个子数组中按降序键排序时保持子数组(班级年份)的顺序。

$pledges = array(
  'smith'     => "Joe Patterson",
  'jones'     => "Robert Nelson",
  'davis'     => "Jimmy Davis",
  'carpenter' => "Mike Carpenter");

$sophomores = array(
  'ford'      => "Kevin Ford",
  'gomez'     => "Pedro Gomez",
  'miller'    => "Jack Miller",
  'pullman'   => "Lucas Pullman");

$juniors = array(
  'bradford'  => "Nicholas Bradford",
  'daniels'   => "Robert Daniels",
  'soren'     => "Jon Soren",
  'cooper'    => "Harrison Cooper");

$seniors = array(
  'mcdonald'  => "Casey McDonald",
  'witten'    => "Tim Witten",
  'session'   => "Benjamin Sessions",
  'redding'   => "Jack Redding");

我该怎么做?非常感谢。

I made this fictional membership list. I'd like to maintain the order of the sub arrays (class year) while sorting by descending key within each sub array.

$pledges = array(
  'smith'     => "Joe Patterson",
  'jones'     => "Robert Nelson",
  'davis'     => "Jimmy Davis",
  'carpenter' => "Mike Carpenter");

$sophomores = array(
  'ford'      => "Kevin Ford",
  'gomez'     => "Pedro Gomez",
  'miller'    => "Jack Miller",
  'pullman'   => "Lucas Pullman");

$juniors = array(
  'bradford'  => "Nicholas Bradford",
  'daniels'   => "Robert Daniels",
  'soren'     => "Jon Soren",
  'cooper'    => "Harrison Cooper");

$seniors = array(
  'mcdonald'  => "Casey McDonald",
  'witten'    => "Tim Witten",
  'session'   => "Benjamin Sessions",
  'redding'   => "Jack Redding");

How do I do that? Thanks a bunch.

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

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

发布评论

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

评论(2

金兰素衣 2024-12-03 23:36:33

PHP 手册有一个很好的例子来说明您想要做什么。

http://www.php.net/manual/en/function.ksort .php#98465

The PHP manual has a good example of what you're looking to do.

http://www.php.net/manual/en/function.ksort.php#98465

葮薆情 2024-12-03 23:36:33

通过使用 PHP 的 ksort 函数,您可以按键对数组进行排序。

ksort($pledges);
array_reverse($pledges);
and like wise for others....

您发布的是单维数组,请注意 ksort 仅适用于单维数组。

by using ksort function of PHP you can sort array key wise.

ksort($pledges);
array_reverse($pledges);
and like wise for others....

what you have posted is single dimension array and be aware that ksort will be only applicable to single dimension array.

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