Home >Backend Development >PHP Tutorial >PHP怎么实现根据数组的值进行分组

PHP怎么实现根据数组的值进行分组

PHPz
PHPzOriginal
2016-06-13 10:06:363076browse

PHP怎么实现根据数组的值进行分组

PHP实现根据数组的值进行分组的方法

Array(
                    [0] => Array
                        (
                            [title] => 新闻投票分成
                            [gold] => +0.004095
                            [create_time] => 1545355432
                            [create_date] => 20181221
                        )
 
                    [1] => Array
                        (
                            [title] => 新闻投票分成
                            [gold] => +0.002801
                            [create_time] => 1545355432
                            [create_date] => 20181221
                        )
 
                    [2] => Array
                        (
                            [title] => 持币分红
                            [gold] => +0.837871
                            [create_time] => 1545194334
                            [create_date] => 20181219
                        )
 
                )

从数据库得到的数据,想根据日期进行分组

 

  $list = array();
            foreach($gold as $key=>$val){
                $val['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$val['create_date']]['time'] = $val['create_date'];
                $list[$val['create_date']]['data'][] = $val;
 
            }
        
            $ret = array();
          
            foreach ($list as $key => $value) {
              array_push($ret, $value);
            }

将得到 的数组循环分组,得到数据

Array
(
    [0] => Array
        (
            [time] => 20181221
            [data] => Array
                (
                    [0] => Array
                        (
                            [title] => 新闻投票分成
                            [gold] => +0.004095
                            [create_time] => 2018-12-21 09:23:52
                            [create_date] => 20181221
                        )
 
                    [1] => Array
                        (
                            [title] => 新闻投票分成
                            [gold] => +0.002801
                            [create_time] => 2018-12-21 09:23:52
                            [create_date] => 20181221
                        )
 
                    [2] => Array
                        (
                            [title] => 新闻投票分成
                            [gold] => +0.002069
                            [create_time] => 2018-12-21 09:23:52
                            [create_date] => 20181221
                        )
                )
 
        )
 
    [1] => Array
        (
            [time] => 20181219
            [data] => Array
                (
                    [0] => Array
                        (
                            [title] => 持币分红
                            [gold] => +0.837871
                            [create_time] => 2018-12-19 12:38:54
                            [create_date] => 20181219
                        )
 
                )
 
        )
 
)

实现根据时间分组,重点在循环的时候定义的类型。

更多相关技术文章,请访问PHP中文网

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