[英]How to sort by key within multidimensional array
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.
谢谢你
The PHP manual has a good example of what you're looking to do. PHP手册很好地说明了您要做什么。
http://www.php.net/manual/en/function.ksort.php#98465 http://www.php.net/manual/zh/function.ksort.php#98465
by using ksort function of PHP you can sort array key wise. 通过使用PHP的ksort函数,可以明智地对数组键进行排序。
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. 您发布的内容是一维数组,请注意,ksort仅适用于一维数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.