您好,登錄后才能下訂單哦!
<?php
/**
* 二維數組根據某個字段排序
* 功能:按照用戶的年齡倒序排序
*/
header('Content-Type:text/html;Charset=utf-8');
$arrUsers = array(
array(
'id' => 1,
'name' => '張三',
'age' => 25,
),
array(
'id' => 2,
'name' => '李四',
'age' => 23,
),
array(
'id' => 3,
'name' => '王五',
'age' => 40,
),
array(
'id' => 4,
'name' => '趙六',
'age' => 31,
),
array(
'id' => 5,
'name' => '黃七',
'age' => 20,
),
);
$sort = array(
'direction' => 'SORT_DESC', //排序順序標志 SORT_DESC 降序;SORT_ASC 升序
'field' => 'age', //排序字段
);
$arrSort = array();
foreach($arrUsers AS $uniqid => $row){
foreach($row AS $key=>$value){
$arrSort[$key][$uniqid] = $value;
}
}
if($sort['direction']){
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $arrUsers);
}
echo '<pre>';
var_dump($arrUsers);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。