PHP数组问题,求助!

WBOY
Release: 2016-06-23 14:00:27
Original
913 people have browsed it

有这样一个数组:
$array = array (
  0 => 
  array (
    'day' => '2014-3-11',
    'count' => 4,
    'day_0' => 3,
    'day_1' => 1,
  ),
  1 => 
  array (
    'day' => '2014-3-7',
    'count' => 2,
    'day_0' => 1,
  ),
  2 => 
  array (
    'day' => '2014-3-6',
    'count' => 4,
    'day_4' => 1,
    'day_5' => 1,
  ),
  3 => 
  array (
    'day' => '2014-3-10',
    'count' => 10,
    'day_0' => 7,
    'day_1' => 2,
    'day_2' => 1,
  ),
);
日期间隔不固定,这里示例是2014-3-6 至2014-3-11,中间间隔五天,想通过对日期的排序和间隔的天数,得到如下数组:


补充:
3月6日与3月11日相差5天,所以3月6日就有day_1、day_2、day_3、day_4、day_5
3月7日与3月11日相差4天,所以3月7日就有day_1、day_2、day_3、day_4
3月8日与3月11日相差3天,所以3月8日就有day_1、day_2、day_3
3月9日与3月11日相差2天,所以3月9日就有day_1、day_2
3月10日与3月11日相差1天,所以3月10日就有day_1
3月11日与3月11日相差0天,所以没有day_……
……依次类推,得出一个类似三角型的数组

请大家帮帮忙吧!!!感谢!


回复讨论(解决方案)

  还没出结果吗?

明天要是还没结果   我帮你写吧

不过我看你上个帖子好像xu大说了方法了啊

  还没出结果吗?

明天要是还没结果   我帮你写吧

不过我看你上个帖子好像xu大说了方法了啊 哎,是啊,还没出来结果呢!这数组处理起来真心麻烦啊!!!xu大说了方法可我写不出来啊!

没看明白,不知道你要干什么。。。

没看明白,不知道你要干什么。。。 仔细看我的数组,想要得出一个类似三角形的数组。

foreach($array as $v){   $days[]=date("Ymd",strtotime($v['day']));   $ar[date("Ymd",strtotime($v['day']))]=$v;}   $arr= range(min($days),max($days));$max = max($arr);foreach($arr as $v){    $d = $max-$v;    if(isset($ar[$v])){	     $tt[$v]['day']=$ar[$v]['day'];		 $tt[$v]['count']=$ar[$v]['count'];		 for($i=1;$i<=$d;$i++) $tt[$v]['day_'.$i]=(int)$ar[$v]['day_'.$i];	}else{	     $tt[$v]['day']=date("Y-m-j",strtotime($v));		 $tt[$v]['count']=0;		 for($i=1;$i<=$d;$i++) $tt[$v]['day_'.$i]=0;	}}print_r(array_values($tt));
Copy after login

Related labels:
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!