php二维数组按某个值数量多的排序 php二维数组中想按某个值数量多的排序如何做呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 详情请查阅手册========================array_multisort(PHP 4, PHP 5)array_multisort -- 对多个数组或多维数组进行排序 例子很容易懂啊?<?php$data[] = array('volume' => 67, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 1);$data[] = array('volume' => 85, 'edition' => 6);$data[] = array('volume' => 98, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 6);$data[] = array('volume' => 67, 'edition' => 7);// 取得列的列表foreach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition'];}// 将数据根据 volume 降序排列,根据 edition 升序排列// 把 $data 作为最后一个参数,以通用键排序array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);?> 那你可以变通变通嘛。粗略的解决方案,可以循环两次,你可以想想如何一次循环就搞定。$data[] = array('volume' => 67, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 1);$data[] = array('volume' => 85, 'edition' => 6);$data[] = array('volume' => 98, 'edition' => 2);$data[] = array('volume' => 86, 'edition' => 6);$data[] = array('volume' => 67, 'edition' => 7);// 取得列的列表$s = array();foreach ($data as $key => $row) { $s[$row['volume']]++;//存储各值出现次数}foreach($data as $key => $row){ $volume[$key] = $s[$row['volume']];//按值出现的次数排序。}print_r($s);// 将数据根据 volume值出现次数 降序排列// 把 $data 作为最后一个参数,以通用键排序array_multisort($volume, SORT_DESC, $data);echo "<pre/>";print_r($data); 解释一下array_multisort(A,B,C)A:原多维数组B:把A中要排序的一项提出来组成的数组c:按什么方式排序(SORT_DESC,SORT_ASC) 使用自定义数组排序函数usort(),你想怎么排序就怎样排序:<?php $arr=array(array('A','b',0),//这是一个乱序的二维数组样本(3列8行) array('B','a',2), array('A','c',1), array('A','a',0), array('B','c',0), array('A','d',1), array('B','a',1), array('A','d',0)); function same_field_count($field_value,$field_no) //定义统计相同字段值的个数,第二个参数表示字段索引号{global $arr;$count=0;foreach($arr as $row){if ($row[$field_no]==$field_value)$count++;}return $count;} function compare($x,$y) /****************************************************************** 自定义二维数组组合排序比较方式 ******************************************************************/ { if(same_field_count($x[2],2)==same_field_count($y[2],2))//此条件中的所有常量"2"是字段索引号,表示第3个字段,可以换成你想对应的字段索引号,后面的条件中也一样return 0; else if(same_field_count($x[2],2)<same_field_count($y[2],2)) return -1; else return 1; } usort($arr,'compare');//执行自定义排序函数 //输出测试结果foreach ($arr as $row) { foreach ($row as $field) echo $field; echo "<br />";}?> 不好意思,用惯了“字段”名称,上面所述的“字段”应称“列”。想实现降序,只要把return -1;和return 1;对调一下即可。 为了减少循环次数,优化了下:<?php $arr=array(array('A','b',0),//这是一个乱序的二维数组样本(3列8行) array('B','a',2), array('A','c',1), array('A','a',0), array('B','c',0), array('A','d',1), array('B','a',1), array('A','d',0)); foreach($arr as $row){$temp0[]=$row[0];$temp1[]=$row[1];$temp2[]=$row[2];}$col_count=array(array_count_values($temp0),array_count_values($temp1),array_count_values($temp2));function compare($x,$y) // 自定义二维数组组合排序比较方式,遗憾的是这个函数只允许接受两个参数,所以不够灵活,{global $col_count; //$countx0=$col_count[0][$x[0]];//$county0=$col_count[0][$y[0]];//$countx1=$col_count[1][$x[1]];//$county1=$col_count[1][$y[1]];$countx2=$col_count[2][$x[2]];$county2=$col_count[2][$y[2]];//下面以第三列排序为例if($countx2==$county2){ return 0; //也可以继续比较其它列,比如countx1和county1等,以达到组合排序的目的{ else if($countx2<$county2) return -1; else return 1; }usort($arr,'compare');?> 3000元改版一套php程序,有意者联系我! 请教一下,我想在每天零点自动运行一条命令,怎么办呢? php获取中文cookie值 有免费的PHP源码加密工具吗? 请较:如何输出二维数组内容 后台留言回复 Why? php解析不正常 高手指点 请教一个菜问题,这个问题也缠了小妹很长时间了,希望能在这里得到答案!谢谢~! 如何实现多站连发 网站 favicon图标 大小不一致 请教csdner,另类方式的注册方式有安全隐患吗?
========================
array_multisort
(PHP 4, PHP 5)array_multisort -- 对多个数组或多维数组进行排序
<?php
$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);// 取得列的列表
foreach ($data as $key => $row) {
$volume[$key] = $row['volume'];
$edition[$key] = $row['edition'];
}// 将数据根据 volume 降序排列,根据 edition 升序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);?>
粗略的解决方案,可以循环两次,你可以想想如何一次循环就搞定。$data[] = array('volume' => 67, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 1);
$data[] = array('volume' => 85, 'edition' => 6);
$data[] = array('volume' => 98, 'edition' => 2);
$data[] = array('volume' => 86, 'edition' => 6);
$data[] = array('volume' => 67, 'edition' => 7);// 取得列的列表
$s = array();
foreach ($data as $key => $row) {
$s[$row['volume']]++;//存储各值出现次数
}
foreach($data as $key => $row)
{
$volume[$key] = $s[$row['volume']];//按值出现的次数排序。
}
print_r($s);
// 将数据根据 volume值出现次数 降序排列
// 把 $data 作为最后一个参数,以通用键排序
array_multisort($volume, SORT_DESC, $data);
echo "<pre/>";
print_r($data);
B:把A中要排序的一项提出来组成的数组
c:按什么方式排序(SORT_DESC,SORT_ASC)
$arr=array(array('A','b',0),//这是一个乱序的二维数组样本(3列8行)
array('B','a',2),
array('A','c',1),
array('A','a',0),
array('B','c',0),
array('A','d',1),
array('B','a',1),
array('A','d',0));
function same_field_count($field_value,$field_no)
//定义统计相同字段值的个数,第二个参数表示字段索引号
{
global $arr;
$count=0;
foreach($arr as $row)
{
if ($row[$field_no]==$field_value)
$count++;
}
return $count;
}
function compare($x,$y)
/******************************************************************
自定义二维数组组合排序比较方式
******************************************************************/
{
if(same_field_count($x[2],2)==same_field_count($y[2],2))
//此条件中的所有常量"2"是字段索引号,表示第3个字段,可以换成你想对应的字段索引号,后面的条件中也一样
return 0;
else if(same_field_count($x[2],2)<same_field_count($y[2],2))
return -1;
else
return 1;
}
usort($arr,'compare');//执行自定义排序函数
//输出测试结果
foreach ($arr as $row)
{
foreach ($row as $field)
echo $field;
echo "<br />";
}
?>
$arr=array(array('A','b',0),//这是一个乱序的二维数组样本(3列8行)
array('B','a',2),
array('A','c',1),
array('A','a',0),
array('B','c',0),
array('A','d',1),
array('B','a',1),
array('A','d',0));
foreach($arr as $row)
{
$temp0[]=$row[0];
$temp1[]=$row[1];
$temp2[]=$row[2];
}
$col_count=array(array_count_values($temp0),array_count_values($temp1),array_count_values($temp2));function compare($x,$y)
// 自定义二维数组组合排序比较方式,遗憾的是这个函数只允许接受两个参数,所以不够灵活,
{
global $col_count;
//$countx0=$col_count[0][$x[0]];
//$county0=$col_count[0][$y[0]];
//$countx1=$col_count[1][$x[1]];
//$county1=$col_count[1][$y[1]];
$countx2=$col_count[2][$x[2]];
$county2=$col_count[2][$y[2]];
//下面以第三列排序为例
if($countx2==$county2)
{
return 0;
//也可以继续比较其它列,比如countx1和county1等,以达到组合排序的目的
{
else if($countx2<$county2)
return -1;
else
return 1;
}
usort($arr,'compare');
?>