数组相关操作

数组排序

二维数组:

(1、)

function cmpx($a, $b){
$al = ($a['giftcount']);
$bl = ($b['giftcount']);
if ($al == $bl){
return 0;
}

return ($al > $bl) ? -1 : 1;
}
usort($zxgift,"cmpx");

2、

$array[] = array('id'=>1,'price'=>50);
$array[] = array('id'=>2,'price'=>70);
$array[] = array('id'=>3,'price'=>30);
$array[] = array('id'=>4,'price'=>20);

foreach ($array as $key=>$value){
$id[$key] = $value['id'];
$price[$key] = $value['price'];
}

array_multisort($price,SORT_NUMERIC,SORT_DESC,$id,SORT_STRING,SORT_ASC,$array);
echo '
';
print_r($array);
echo '
';
?>

运行结果:

Array
(
[0] => Array
(
[id] => 2
[price] => 70
)

[1] => Array
(
[id] => 1
[price] => 50
)

[2] => Array
(
[id] => 3
[price] => 30
)

[3] => Array
(
[id] => 4
[price] => 20
)

)

转载于:https://www.cnblogs.com/lvzhihui/p/4750274.html

你可能感兴趣的:(php)