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

WBOY
Release: 2016-06-06 20:33:32
Original
1024 people have browsed it

<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>
Copy after login
Copy after login

'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>
Copy after login
Copy after login

'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>
Copy after login

可以写下自定义的排序

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!