PHP 无框架 根据条件组成数组

Sayonara1 发布于 2020/05/11 10:51
阅读 197
收藏 0

[{"id":"33","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"11","first_time":"2020-05-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"34","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"7","first_time":"2020-05-11 07:53:26","name":"\u5546\u54c1\u4e00"},{"id":"35","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"7","first_time":"2020-05-11 07:55:44","name":"\u5546\u54c1\u4e00"},{"id":"36","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"1","first_time":"2020-05-11 08:00:48","name":"\u5546\u54c1\u4e00"},{"id":"37","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"2","first_time":"2020-05-11 08:02:34","name":"\u5546\u54c1\u4e00"},{"id":"38","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"1","first_time":"2020-05-11 08:06:57","name":"\u5546\u54c1\u4e00"},{"id":"39","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"1","first_time":"2020-05-11 08:07:56","name":"\u5546\u54c1\u4e00"},{"id":"40","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"1","first_time":"2020-05-11 08:09:51","name":"\u5546\u54c1\u4e00"},{"id":"41","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"2","first_time":"2020-05-11 08:10:56","name":"\u5546\u54c1\u4e00"},{"id":"42","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"1","first_time":"2020-05-11 08:11:46","name":"\u5546\u54c1\u4e00"},{"id":"43","cate_id":"1","year":"2020","moon":"05","day":"11","hits":"2","first_time":"2020-05-11 08:15:49","name":"\u5546\u54c1\u4e00"},{"id":"44","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"45","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"46","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"47","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"48","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"49","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"50","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"51","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"52","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"53","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"54","cate_id":"1","year":"2020","moon":"06","day":"11","hits":"1","first_time":"2020-06-11 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"55","cate_id":"1","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"56","cate_id":"1","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"57","cate_id":"1","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"58","cate_id":"1","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e00"},{"id":"59","cate_id":"2","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e8c"},{"id":"60","cate_id":"2","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e8c"},{"id":"61","cate_id":"2","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e8c"},{"id":"62","cate_id":"2","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e8c"},{"id":"63","cate_id":"2","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e8c"},{"id":"64","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"65","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"66","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"67","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"68","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"69","cate_id":"3","year":"2020","moon":"07","day":"01","hits":"1","first_time":"2020-07-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"70","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"71","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"72","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"73","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"74","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"75","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"76","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"77","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"78","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"79","cate_id":"3","year":"2020","moon":"08","day":"01","hits":"1","first_time":"2020-08-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"80","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"81","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"82","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"83","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"84","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"85","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"86","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"87","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"88","cate_id":"3","year":"2020","moon":"09","day":"01","hits":"1","first_time":"2020-09-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"89","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"90","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"91","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"92","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"93","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"94","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"95","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"96","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"97","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"98","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"99","cate_id":"3","year":"2020","moon":"10","day":"01","hits":"1","first_time":"2020-10-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"100","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"101","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"102","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"103","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"104","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"105","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"106","cate_id":"3","year":"2020","moon":"11","day":"01","hits":"1","first_time":"2020-11-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"107","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"108","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"109","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"110","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"111","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"112","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"},{"id":"113","cate_id":"3","year":"2020","moon":"12","day":"01","hits":"1","first_time":"2020-12-01 07:51:14","name":"\u5546\u54c1\u4e09"}]

 

我想做一个报表统计的,查询出来的数据如上。 希望得到这种结果。

按年份(year)输出 分类(cate_id) 12个月(moon)的 点击数量 hits 的值

        [{
            cate_id: '1',
            data: [10, 20, 30, 40, 50, 60, 70, 80, 70, 60, 50, 40]
        }, {
            cate_id: '2',
            data: [30, 40, 45, 50, 55, 40, 30, 28, 17, 55, 35, 22]
        }, {
            cate_id: '3',
            data: [29, 46, 56, 66, 46, 38, 28, 47, 53, 33, 17, 40]
        }, {
            cate_id: '4',
            data: [39, 42, 40, 55, 65, 45, 65, 70, 90, 103, 66, 48]
        }]    

加载中
0
Fize
Fize

用for和if就能搞定的事情,自己想办法解决,和什么框架没有关系。

0
tcxu
tcxu

这里,用 js 开个头。楼主的数据存入数组 dat:

dat = [{"id":"33","cate_id":"1","year":"2020","moon":"05","day":"11",
  "hits":"11","first_time":"2020-05-11 07:51:14","name":"\u5546\u54c1\u4e00"},
  {"id":"34","cate_id":"1","year":"2020","moon":"05","day":"11",
  "hits":"7","first_time":"2020-05-11 07:53:26","name":"\u5546\u54c1\u4e00"},
  ...]

hit =new Array(0,0,0,0,0,0,0,0,0,0,0,0,0);

for ($i=0;$i<dat.length; $i++){ //1-4月没有数据提供。
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 5 ))
	hit[5] += parseInt(dat[$i].hits);

	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 6 ))
	hit[6] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 7 ))
	hit[7] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 8 ))
	hit[8] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 9 ))
	hit[9] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon ==10 ))
	hit[10] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 11 ))
	hit[11] += parseInt(dat[$i].hits);
	
	if ((dat[$i].cate_id == 1) && ( dat[$i].moon == 12 ))
	hit[12] += parseInt(dat[$i].hits);
	}
	 
res = [{"cate_id":"1", "data": hit}];
alert( "cate_id: " + res[0].cate_id + ", data: " + res[0].data )

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部