0

I know similar questions to this have been asked, but even looking at all of them, I can't seem to get this to work. I think it's just a tad bit more complex than the other examples I'm finding. I know someone is going to say it's a repeat question - but I've tried really hard to get it from the examples I've seen so far - so sorry in advance!

So given this multidimensional array $results_display in PHP (var_dump below), there are 5 members of the sub-array "#results", and I want to sort (descending) those 5 by the value in the "#changed" string.

Can someone please help a girl out who's been banging her head against her desk for a couple days?

Thank you so much!!!

What I tried is below the var_dump. I commented out the part with the title to try and get just the first part working.

$results_display =
array(8) {
   ["#theme"]=> string(18) "hs_filters_results"
   ["#title"]=> string(18) "On-Demand Webinars"
   ["#body"]=> NULL
   ["#results"]=> array(5) {
     [0]=> array(3) {      
     ["#changed"]=> string(10) "1403279484"
     ["#theme"]=> string(17) "hs_filters_result"
     ["#result"]=> array(25) {
        ["#nid"]=> string(4) "2057"
        ["#node_type"]=> array(2) {
           ["machine_name"]=> string(7) "webinar"
           ["name"]=> string(7) "Webinar" }
        ["#title"]=> string(61) "7 Critical Reasons to Automate Handling of IBM i Spool Files "
        ["#brand_nid"]=> string(2) "29"
        ["#brand_machine_name"]=> string(5) "brand"
        ...  }
   }
...

}

// Obtain a list of columns for the results array
foreach ($results_display as $key => $row) {
  $changed[$key]  = $row['changed'];
  //$title[$key] = $row['title'];
} 

// Sort the data with date changed descending, title ascending
// Add $results_display as the last parameter, to sort by the common key
//array_multisort($changed, SORT_DESC, $title, SORT_ASC, $results_display);
array_multisort($changed, SORT_DESC, $results_display);
Pamela
  • 1
  • 1
  • use `usort`, and where are those codes that you're working on – Kevin Mar 04 '15 at 06:25
  • And to add one more wrinkle to this, I would also like to sort by the #title string (ascending) within the #result sub-array of the #results array. So first sort by the changed date, but if two of the results have the same changed date, sort those by the title. – Pamela Mar 04 '15 at 06:30
  • I also tried a uasort like this: – Pamela Mar 04 '15 at 06:46
  • function cmpi($a, $b) { return strcmp($a["results"]["changed"], $b["results"]["changed"]); } // do the array sorting uasort($results_display, 'cmpi'); – Pamela Mar 04 '15 at 06:46
  • I'm sorry my formatting is so bad. :( – Pamela Mar 04 '15 at 06:48

1 Answers1

0
usort($results_display['results'], function ($a, $b) {
    return $a['changed'] - $b['changed'];
});

This should get you started. For more possible sorting options, see https://stackoverflow.com/a/17364128/476.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for you help deceze! I will look at your reference above...but I tried this usort code, and just got the error: "Warning: usort() expects parameter 1 to be array, null given in..." Which doesn't make a lot of sense, because $results_display is clearly an array. – Pamela Mar 04 '15 at 16:20
  • Well, that means `$results_display['results']` is not the array you're looking for. Maybe it's indeed `$results_display['#results']`? – deceze Mar 04 '15 at 17:11