Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort PHP multi-dimensional array based on value in inner array? [duplicate]

Tags:

php

sorting

I'm trying to sort my PHP hashtable based on a specific value in the inner array. The data structure looks like this:

print_r($mydata);  Array( [0] => Array     (         [type] => suite         [name] => A-Name     ) [1] => Array     (         [type] => suite         [name] => C-Name     ) [2] => Array     (         [type] => suite         [name] => B-Name     ) ) 

I've tried ksort, sort, usort but nothing seems to work. I'm trying to sort based on the name key two-levels down.

This was my attempt using usort:

function cmp($a, $b) {     return $b['name'] - $a['name']; }  usort($mydata, "cmp"); 

Is there an easy way to do this or do I need to write a custom sort function?

like image 787
user1216398 Avatar asked Apr 30 '13 18:04

user1216398


2 Answers

Thinking,more useful and practical http://php.net/manual/en/function.sort.php

function array_sort($array, $on, $order=SORT_ASC){      $new_array = array();     $sortable_array = array();      if (count($array) > 0) {         foreach ($array as $k => $v) {             if (is_array($v)) {                 foreach ($v as $k2 => $v2) {                     if ($k2 == $on) {                         $sortable_array[$k] = $v2;                     }                 }             } else {                 $sortable_array[$k] = $v;             }         }          switch ($order) {             case SORT_ASC:                 asort($sortable_array);                 break;             case SORT_DESC:                 arsort($sortable_array);                 break;         }          foreach ($sortable_array as $k => $v) {             $new_array[$k] = $array[$k];         }     }      return $new_array; } 

How to use

 $list = array(    array( 'type' => 'suite', 'name'=>'A-Name'),    array( 'type' => 'suite', 'name'=>'C-Name'),    array( 'type' => 'suite', 'name'=>'B-Name')  );  $list = array_sort($list, 'name', SORT_ASC);  array(3) { [0]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "A-Name"    } [2]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "B-Name" } [1]=> array(2) { ["type"]=> string(5) "suite" ["name"]=> string(6) "C-Name" } } 
like image 118
voodoo417 Avatar answered Sep 18 '22 17:09

voodoo417


Try this usort function:

    function cmp($a, $b){         if ($a == $b)             return 0;         return ($a['name'] < $b['name']) ? -1 : 1;     }  $my_array = array( 0 => array     (         'type' => 'suite'         ,'name' => 'A-Name'     ) ,1 => array     (         'type' => 'suite'         ,'name' => 'C-Name'     ) ,2 => array     (         'type' => 'suite'         ,'name' => 'B-Name'     ) );   usort($my_array, "cmp"); 

If you using it in a class, the second parameter changes to an array like this:

usort($my_array, array($this,'cmp')); 
like image 30
Ali Seyfollahi Avatar answered Sep 19 '22 17:09

Ali Seyfollahi