Created
March 6, 2013 19:12
-
-
Save dodopok/5102110 to your computer and use it in GitHub Desktop.
Sort Multiarray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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