Skip to content

Instantly share code, notes, and snippets.

@dodopok
Created March 6, 2013 19:12
Show Gist options
  • Save dodopok/5102110 to your computer and use it in GitHub Desktop.
Save dodopok/5102110 to your computer and use it in GitHub Desktop.
Sort Multiarray
<?php
$songs = array(
'1' => array('artist'=>'The Smashing Pumpkins', 'songname'=>'Soma'),
'2' => array('artist'=>'The Decemberists', 'songname'=>'The Island'),
'3' => array('artist'=>'Fleetwood Mac', 'songname' =>'Second-hand News')
);
function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[] = $a[$key];
}
return $c;
}
$songs = subval_sort($songs,'artist');
print_r($songs);
?>
//Resultado
Array
(
[0] => Array
(
[artist] => Fleetwood Mac
[song] => Second-hand News
)
[1] => Array
(
[artist] => The Decemberists
[song] => The Island
)
[2] => Array
(
[artist] => The Smashing Pumpkins
[song] => Cherub Rock
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment