-1

I'm trying to sort the output value that's returned by greatest to least.

Here's an example of the array:

array(
    'ACTION' => 'getsupportdepartments',
    'RESULT' => 'success',
    'TOTALRESULTS' => 2,
    'DEPARTMENTS' => array(
      'DEPARTMENT' => array(
        'ID' => 2,
        'NAME' => 'Sales',
        'AWAITINGREPLY' => 5,
        'OPENTICKETS' => 5
      ),
      'DEPARTMENT1' => array(
        'ID' => 1,
        'NAME' => 'Support',
        'AWAITINGREPLY' => 15,
        'OPENTICKETS' => 15

The code I'm using is:

if(!empty($_GET['sort'])) {
  $tmpArray = array();
  foreach($arr['WHMCSAPI']['DEPARTMENTS'] as $key => $value) {
    $tmpArray[$value['AWAITINGREPLY']] = $value;
  }
  $arr['WHMCSAPI']['DEPARTMENTS'] = $tmpArray;

  ($_GET['sort'] == 'desc') ? krsort($arr['WHMCSAPI']['DEPARTMENTS']) : ksort($arr['WHMCSAPI']['DEPARTMENTS']);
}


    ($_GET['sort'] == 'desc') ? krsort($arr['WHMCSAPI']['DEPARTMENTS']) : ksort($arr['WHMCSAPI']['DEPARTMENTS']);

  foreach($arr['WHMCSAPI']['DEPARTMENTS'] as $department) {
    echo $department['NAME'].' - '.$department['AWAITINGREPLY'].'<br />';
 }
  echo $exc;

However the order of the output from AWAITINGREPLY is not sorting.

hakre
  • 178,314
  • 47
  • 389
  • 754
cbcp
  • 299
  • 1
  • 6
  • 12
  • Could you be more specific about what is your desired output? – Passerby Jun 15 '13 at 03:02
  • Currently, the output is: Sales - 5 Support - 15 I would like to sort largest -> smallest Support - 15 Sales - 5 – cbcp Jun 15 '13 at 03:04
  • So you want to sort the `['DEPARTMENTS']` array, order by `['AWAITINGREPLY']`? – Passerby Jun 15 '13 at 03:06
  • Sorry, but this has likely been asked before, please first search the site. Also what has this to do with XML and API? I removed the tags because for the programming questions, this has nothing to do with those. – hakre Jun 15 '13 at 08:39

1 Answers1

0

No need to reinvent the wheel, there's already a function called usort:

online demo

function sortDepts(array &$arr,$sort="desc")
{
    if($sort=="desc")
        usort($arr["DEPARTMENTS"],function($a,$b){
            return $b["AWAITINGREPLY"]-$a["AWAITINGREPLY"];
        });
    else
        usort($arr["DEPARTMENTS"],function($a,$b){
            return $a["AWAITINGREPLY"]-$b["AWAITINGREPLY"];
        });
    return $arr;
}

Note that anonymous function works in PHP>=5.3. If you need to support 5<=PHP<5.3, you'll need to declare the function first.


Edit:

For 5 < PHP < 5.3:

online demo

function asc($a,$b)
{
    return $a["AWAITINGREPLY"]-$b["AWAITINGREPLY"];
}
function desc($a,$b)
{
    return $b["AWAITINGREPLY"]-$a["AWAITINGREPLY"];
}
function sortDepts(array &$arr,$sort="desc")
{
    if($sort=="desc")
        usort($arr["DEPARTMENTS"],"desc");
    else
        usort($arr["DEPARTMENTS"],"asc");
    return $arr;
}

Edit #2:

In case you still need the key (DEPARTMENT,DEPARTMENT1,etc.), use uasort instead of usort (no other code change is required).

Passerby
  • 9,184
  • 2
  • 28
  • 44