<?php 
///多维数组排序,按照某个字段进行排序
header('Content-Type: text/html; charset=utf-8'); 

echo '<pre>'; 
//原始数组格式 
$array = array( 
    'key1' => array( 
        'item1' => '65', 
        'item2' => '35', 
        'item3' => '84', 
    ), 
    'key2' => array( 
        'item1' => '24', 
    ), 
    'key3' => array( 
        'item1' => '38', 
        'item3' => '45', 
    ), 
); 

//要排序的键 
//按照数组中的 item1进行排序 
$sort = 'item1'; 
foreach($array as $k => $v)  { 
    $newArr[$k] = $v[$sort];
} 
//这个函数如果执行正确他会直接改变原数组键值的顺序 
//如果执行失败,那么他会返回 bool(false) 
array_multisort($newArr,SORT_ASC, $array); 
print_r($array); 
echo '</pre>';

/***
///////输出结果
Array
(
    [key2] => Array
        (
            [item1] => 24
        )

    [key3] => Array
        (
            [item1] => 38
            [item3] => 45
        )

    [key1] => Array
        (
            [item1] => 65
            [item2] => 35
            [item3] => 84
        )

)
**/
?> 

 

posted on 2014-02-27 14:17  IT使者  阅读(336)  评论(0编辑  收藏  举报