如何在多维数组中按键排序
我制作了这个虚构的会员名单。我想在每个子数组中按降序键排序时保持子数组(班级年份)的顺序。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
通过使用 PHP 的 ksort 函数,您可以按键对数组进行排序。
您发布的是单维数组,请注意 ksort 仅适用于单维数组。
by using ksort function of PHP you can sort array key wise.
what you have posted is single dimension array and be aware that ksort will be only applicable to single dimension array.