0

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.

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.