Friday, February 17, 2012

Sorting a Multi-Dimensional Array with PHP


//an array of some songs I like
$employee =  array(
  '1' => array('name'=>'Hanzala Subhani', 'job_title'=>'Software Developer'),
  '2' => array('name'=>'Safdar Subhani', 'job_title'=>'Software Developer'),
  '3' => array('name'=>'Shamsh Jawed', 'job_title' =>'Manager')
 );

function sort_employee($a,$subkey) {
 foreach($a as $k=>$v) {
  $b[$k] = strtolower($v[$subkey]);
 }
 asort($b);
 foreach($b as $key=>$val) {
  $c[] = $a[$key];
 }
 return $c;
}


$employee = sort_employee($employee,'artist'); 
print_r($employee);

No comments:

Post a Comment