21,890
社区成员
发帖
与我相关
我的任务
分享$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);<?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);
?>
<?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');
?>
<?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 />";
}
?>