本文目录一览:
php数组对象元素排序问题
$fruits =array( 0 = array( 'id' = 1, 'type' = '普通发票', 'weight' = 1, 'is_start' = 1), 1 = array( 'id' = 2, 'type' = '增值税专用发票', 'weight' = 2, 'is_start' = 1), 2 = array( 'id' = 3, 'type' = '收据', 'weight' = 0, 'is_start' = 1), 3 = array( 'id' = 4, 'type' = '测试', 'weight' = 4, 'is_start' = 0) ); var_dump($fruits); function compare($x,$y) { if($x['weight'] == $y['weight']) return 0; elseif($x['weight'] $y['weight']) return -1; else return 1; } usort($fruits,"compare"); echo $fruits[0]['id']."br /"; echo $fruits[1]['id']."br /"; echo $fruits[2]['id']."br /"; echo $fruits[3]['id']."br /";
关于PHP数组排序方法
告诉你一个简单方法:(我给你思路)
$arrays
=
array(
'123厢dgdsds','56dggrff','8955dgc','7gdffd'
);
你foreach
一下,比如:
foreach($arrays
as
$str){
$str
=
(int)$str;
//
这样,就强制把字符串转化成整数了,字母部分会被省略
}
然后,用php
自带的数组排序函数,一下搞定。。。
----------------------
^_*
,谢谢哦。
PHP 对数组排序
function sort3wei($array){
foreach($array as $key=$val){
$val_array= array();
$sort_array = array();
foreach($val as $k=$v){
$val_array[] = $v;
$sort_array[] = $v['orders'];
}
sort($sort_array);
foreach($sort_array as $k=$v){
$array[$key][$k] = $val_array[$k];
}
}
}
在php中如何对一个数组进行升序和降序排列?
升序:sort()函数
降序:rsort()函数
?php
$people=array('name','sex','nation','birth');
foreach ($people as $mychrs)
echo $mychrs." ";
sort($people);
echo "br /---排序后---br /";
foreach ($people as $mychrs)
echo $mychrs." ";
?
PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如:
$num1=range(1,9);
rsort($num1);
这里其实就相当于range(9,1)