Home > Backend Development > PHP Tutorial > PHP数组有关问题,

PHP数组有关问题,

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 11:52:01
Original
877 people have browsed it

PHP数组问题,求助!
有这样一个数组:
$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_……
……依次类推,得出一个类似三角型的数组

请大家帮帮忙吧!!!感谢!
------解决方案--------------------

foreach($array as $v){<br />   $days[]=date("Ymd",strtotime($v['day']));<br />   $ar[date("Ymd",strtotime($v['day']))]=$v;<br />}   <br />$arr= range(min($days),max($days));<br />$max = max($arr);<br />foreach($arr as $v){<br />    $d = $max-$v;<br />    if(isset($ar[$v])){<br />	     $tt[$v]['day']=$ar[$v]['day'];<br />		 $tt[$v]['count']=$ar[$v]['count'];<br />		 for($i=1;$i<=$d;$i++) $tt[$v]['day_'.$i]=(int)$ar[$v]['day_'.$i];<br />	}else{<br />	     $tt[$v]['day']=date("Y-m-j",strtotime($v));<br />		 $tt[$v]['count']=0;<br />		 for($i=1;$i<=$d;$i++) $tt[$v]['day_'.$i]=0;<br />	}<br />}<br />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