开发者

PHP sorting according sub array [duplicate]

开发者 https://www.devze.com 2023-03-24 23:43 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Sorting a multidimensional array in PHP?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Sorting a multidimensional array in 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')
);

Answer should come like this:

   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
    )
)

Please help me on this.


I know php

Try This :

 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;
 }

call to

 $songs = subval_sort($songs,'artist'); 
 print_r($songs);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号