3

I have an array that looks like this:

array(3) { 
    ["Fall Quarter 2012"]=> array(2) { 
          [20121018]=> array(1) { 
              ["agenda"]=> string(55) "Fall_2012/Agenda_20121018.pdf" 
          }
          [20121011]=> array(2) { 
              ["agenda"]=> string(55) "Fall_2012/Agenda_20121011.pdf" 
              ["minutes"]=> string(56) "Fall_2012/Minutes_20121011.pdf" 
          } 
    } 
    ["Spring Quarter 2012"]=> array(1) { 
          [20120413]=> array(1) { 
              ["agenda"]=> string(57) "SPRing_2012/Agenda_20120413.pdf" 
          } 
    } 
    ["Summer Quarter 2012"]=> array(1) { 
          [20120610]=> array(2) { 
              ["agenda"]=> string(57) "Summer_2012/Agenda_20120610.pdf" 
              ["minutes"]=> string(58) "Summer_2012/Minutes_20120610.pdf" 
          } 
    } 
 }

And I'd like to sort it using date keys so that quarters are in the right order Fall/Summer/Spring. Which should look like this:

array(3) { 
    ["Fall Quarter 2012"]=> array(2) {
          [20121018]=> array(1) { 
              ["agenda"]=> string(55) "Fall_2012/Agenda_20121018.pdf" 
          }
          [20121011]=> array(2) { 
              ["agenda"]=> string(55) "Fall_2012/Agenda_20121011.pdf" 
              ["minutes"]=> string(56) "Fall_2012/Minutes_20121011.pdf" 
          } 
    }
    ["Summer Quarter 2012"]=> array(1) { 
          [20120610]=> array(2) { 
              ["agenda"]=> string(57) "Summer_2012/Agenda_20120610.pdf" 
              ["minutes"]=> string(58) "Summer_2012/Minutes_20120610.pdf" 
          } 
    } 
    ["Spring Quarter 2012"]=> array(1) { 
          [20120413]=> array(1) { 
              ["agenda"]=> string(57) "SPRing_2012/Agenda_20120413.pdf" 
          } 
    } 
 }

Is there a way to get this result by sorting using date, or should I use uksort() function to sort quarters with my own pattern?

Please, let me know what you think! Thank you!

1 Answer 1

3

you should use the uasort function. Your code could look something like this:

function myComparison($a, $b){
    return (key($a) > key($b)) ? -1 : 1;
} 

uasort ( $quarters , 'myComparison' );

That will sort using $quarters["Fall Quarter 2012"]["20121018"] for example

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

2 Comments

Thanks! This sorts fine but how do I keep quarters' names? When I sort like this it turns my keys (Fall Quarter 2012/Summer Quarter 2012/ Spring Quarter 2012) into (0/1/2). Also it should be ">" and not "<".
And also changed the < to a > in the comparison function

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.