进出多维数组

时间:2015-07-10 14:56:38

标签: php arrays multidimensional-array

我有一个看起来像这样的大型多维数组(日期值是年 - 月 - 日):

$current_branch = array(
            array('2014-05-26',100,17600,20600,3000,80,15000,18000, 2000),
            array('2014-05-27',100,17600,20600,3000,80,15000,18000, 2000),
            array('2014-05-28',100,17600,20600,3000,80,15000,18000, 2000),
            array('2014-05-29',100,16600,20600,3000,80,15000,18000, 2000),
            array('2014-05-30',100,17600,20600,3000,80,15000,18000, 2000),
            array('2014-05-31',100,17600,20600,3000,80,15000,18000, 2000),
            array('2014-06-02',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-03',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-04',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-05',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-07',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-08',100,100,20600,3000,80,15000,18000, 2000),
            array('2014-06-09',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-06-10',100,12000,20600,3000,80,15000,18000, 2000),
            array('2014-07-01',100,11000,20600,3000,80,15000,18000, 2000),
            array('2014-07-02',100,14000,20600,3000,120,15000,18000, 2000),
            array('2014-07-03',90,12000,20300,3000,140,15000,18000, 2000),
            array('2014-07-04',90,12000,20300,3000,140,15000,18000, 2000),
            array('2014-07-05',90,12000,20300,3000,140,15000,18000, 2000),
            array('2014-07-06',80,11000,19300,3000,140,15000,18000, 2000),
            array('2014-07-07',130,11000,19300,3000,140,15000,20000, 2000),
            array('2014-07-08',130,11000,19300,3000,140,15000,20000, 2000),
            array('2014-07-09',120,10000,19200,3100,140,16000,21000, 2000),
            array('2015-05-01',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-05-02',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-05-03',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-05-04',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-05-05',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-06-01',100,17200,20600,3000,80,15000,18000, 2000),
            array('2015-06-02',100,17300,20600,3000,80,15000,18000, 2000),
            array('2015-06-03',100,17400,20600,3000,80,15000,18000, 2000),
            array('2015-06-04',100,17500,20600,3000,80,15000,18000, 2000),
            array('2015-06-06',100,17700,20600,3000,80,15000,18000, 2000),
            array('2015-06-07',100,17800,20600,3000,80,15000,18000, 2000),
            array('2015-06-08',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-06-09',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-06-10',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-06-20',100,17600,20600,3000,80,15000,18000, 2000),
            array('2015-06-30',144,14600,20600,3000,80,15000,18000, 2000),
            array('2015-07-01',144,11600,17600,2000,80,13000,12000, 5000),
            array('2015-07-02',144,14600,20600,3000,80,15000,18000, 2000),
            array('2015-07-03',154,15600,21600,4000,90,15000,18000, 2000),
            array('2015-07-04',154,15600,21600,4000,90,15000,18000, 2000),
            array('2015-07-05',154,15600,21600,4000,90,15000,18000, 2000),
            array('2015-07-06',124,13600,20600,4000,90,15000,18000, 2000),
            array('2015-07-07',124,2600,600,28000,90,15000,18000, 2000),
            array('2015-07-08',124,2300,21600,3000,90,14000,19000, 2100),
            array('2015-07-09',124,2800,21600,4100,90,15000,18000, 2000)
            );

如您所见,某些日期等缺失。现在,数组索引已经为我正在做的不可思议的数字(使用预定义的soap服务器的结果),但这很好,因为心理记录可以由值组成,并且它们是是;销售日期,现金销售数量,当天现金净额等......

我想知道的是我如何操纵那个庞大的数组以获取其中月份的总净值(例如,可以说第3个值是那天的净值)和输出它们是一个看起来像这样的数组:

$data = array(
  array('jan', $jan_month_net), array('feb', $feb_month_net), 
  array('mar', $mar_month_net), array('apr', ...), 
  array('may', ...), array('jun', ...), 
  array('jul', ...), array('aug', ...), 
  array('sept', ...), array('oct', ...), 
  array('nov', ...),  array('dec', ...)
);

我尝试过使用foreach:

$this_year_value = 2015;
$i=01;
foreach ($current_branch as $key => $value) {
    $i_padded = sprintf("%02d", $i); // so 01, 02, 03, etc
    $month_comparison = $this_year_value."-".$i_padded;
    $cut_soap_date = substr($value[0],0,7); // try to get 2015-MONTH
    if ($cut_soap_date == $month_comparison) { 
        $year_chart_data =  array($i_padded, $value[2]);
    }
$i++; 
}

这段代码有一个根本的缺陷,因为$ i会在每个foreach处递增,实际上从未提供与当前foreach月指针的正确比较。这是我缺乏想法的地方,如果有任何想法可以提供,那就太棒了。

2 个答案:

答案 0 :(得分:0)

如果日期不匹配,只增加$ i怎么样

if ($cut_soap_date == $month_comparison) { 
    $year_chart_data =  array($i_padded, $value[2]);
} else {
    $i++; 
}

答案 1 :(得分:0)

$this_year_value = 2015;

// Make array before
$data = array(
  array('jan', 0), array('feb', 0), 
  array('mar', 0), array('apr', 0), 
  array('may', 0), array('jun', 0), 
  array('jul', 0), array('aug', 0), 
  array('sept', 0), array('oct', 0), 
  array('nov', 0),  array('dec', 0)
);

// fill array 
foreach ($current_branch as $key => $value) {
    // take second part of date
    $month_comparison = explode('-', $value[0])[1];
    $cut_soap_date = substr($value[0],0,7); // try to get 2015-MONTH
    if ($cut_soap_date == $this_year_value."-".$month_comparison) { 
        // write to result array
        $data[$month_comparison-1][1] += $value[2];
    }
}

var_export($data);