Home  >  Article  >  Backend Development  >  PHP如何把计算好的count总数排列最前面?

PHP如何把计算好的count总数排列最前面?

WBOY
WBOYOriginal
2016-06-06 20:33:321025browse

<code>$arr = Array(
   Array
        (
            'department' => '雅典娜',
             'sign' => 0,
            'time' => '2015年06月01日',
            'surname' => '星史'
        )
,
     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月02日',
            'surname' =>  '星史'
        )
,
     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月03日',
            'surname' => '瞬'
        ),

     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月03日',
            'surname' => '紫龙'
        )
);
$array=array();
foreach($arr as $v){
    if(!isset($array[$v['surname']])){
              $array[$v['surname']]['count']=1;
              $array[$v['surname']]['time']=$v['time'];
    }else{
         $array[$v['surname']]['count']+=1;
         $array[$v['surname']]['time'].=','.$v['time'];
    }
}
print_r($array);
````

输出结果是

</code>

'surname' = '星史',
'count'=2

其它
'surname' => '瞬' ,
'count'=1都是1

但是这个输出默认不排序的

```

如何把'count'总数最大排列前面呢?

回复内容:

<code>$arr = Array(
   Array
        (
            'department' => '雅典娜',
             'sign' => 0,
            'time' => '2015年06月01日',
            'surname' => '星史'
        )
,
     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月02日',
            'surname' =>  '星史'
        )
,
     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月03日',
            'surname' => '瞬'
        ),

     Array
        (
            'department' => '雅典娜',
            'sign' => 0,
            'time' => '2015年06月03日',
            'surname' => '紫龙'
        )
);
$array=array();
foreach($arr as $v){
    if(!isset($array[$v['surname']])){
              $array[$v['surname']]['count']=1;
              $array[$v['surname']]['time']=$v['time'];
    }else{
         $array[$v['surname']]['count']+=1;
         $array[$v['surname']]['time'].=','.$v['time'];
    }
}
print_r($array);
````

输出结果是

</code>

'surname' = '星史',
'count'=2

其它
'surname' => '瞬' ,
'count'=1都是1

但是这个输出默认不排序的

```

如何把'count'总数最大排列前面呢?

使用PHP的usort函数进行自定义排序

<code><?php function my_sort($a, $b){
        return $a['count'] == $b['count'] ? 0 : ($a['count'] > $b['count'] ? -1 : 1);
    }
    $array = array(...); //array为您的数据数组
    usort($array, 'my_sort');
    print_r($array);
</code>

可以写下自定义的排序

<code>php</code><code>function my_sort($a, $b)
  {
    return -($a['count'] - $b['count']);//降序
  }

 uasort($array, "my_sort");
print_r($array);
</code>

两个人答案不错,可惜不能点两个采纳了~~~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn