php二维数组中想按某个值数量多的排序如何做呢?

解决方案 »

  1.   

    详情请查阅手册
    ========================
    array_multisort
    (PHP 4, PHP 5)array_multisort -- 对多个数组或多维数组进行排序
      

  2.   

    例子很容易懂啊?
    <?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);?>
      

  3.   

    那你可以变通变通嘛。
    粗略的解决方案,可以循环两次,你可以想想如何一次循环就搞定。$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);
      

  4.   

    解释一下array_multisort(A,B,C)A:原多维数组
    B:把A中要排序的一项提出来组成的数组
    c:按什么方式排序(SORT_DESC,SORT_ASC)
      

  5.   

    使用自定义数组排序函数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 />";
    }
    ?> 
      

  6.   

    不好意思,用惯了“字段”名称,上面所述的“字段”应称“列”。想实现降序,只要把return -1;和return 1;对调一下即可。
      

  7.   

    为了减少循环次数,优化了下:<?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');
    ?>