1

How to sort an entire multi dimensional array in php based on any one of the index?

Like i have an array say $a=array('name'=>'sandep','dob'=>'06/08/1987','education'=>'undergrad'); i have data filled into this array based on the index. Now i want to sort the entire array data based on any one of the index. Say 'DOB'. Is it possible to do them in PHP?

EG: if i have thousand rows. The entire array should be a sorted one based on DOB.

user938588
  • 41
  • 3
  • 1
    possible duplicate of [PHP sort multidimensional array by value](http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value) and http://stackoverflow.com/search?q=%5Bphp%5D+sort+multidimensional – JJJ Oct 22 '11 at 17:50

2 Answers2

0

Yes, you can use usort to use your own sort function.

xdazz
  • 158,678
  • 38
  • 247
  • 274
0
<?php 
$multiArray = Array( 
    Array("id" => 1, "name" => "Defg"), 
    Array("id" => 2, "name" => "Abcd"), 
    Array("id" => 3, "name" => "Bcde"), 
    Array("id" => 4, "name" => "Cdef")); 
$tmp = Array(); 
foreach($multiArray as &$ma) 
    $tmp[] = &$ma["name"]; 
array_multisort($tmp, $multiArray); 
foreach($multiArray as &$ma) 
    echo $ma["name"]."<br/>"; 

/* Outputs 
    Abcd 
    Bcde 
    Cdef 
    Defg 
*/ 
?>
Wazy
  • 8,822
  • 10
  • 53
  • 98