具有一定条件的关联数组元素的求和

时间:2015-07-23 06:40:08

标签: php multidimensional-array yii associative-array

我在php中有数组,如下所示

Array
(
[2015-07-22 09:00@#@2015-07-22 10:00] => Array
        (
            [17:30] => 1
        )

    [2015-07-22 10:00@#@2015-07-22 11:00] => Array
        (
            [17:30] => 2
        )

    [2015-07-22 11:00@#@2015-07-22 12:00] => Array
        (
            [17:30] => 8
        )

    [2015-07-22 12:00@#@2015-07-22 13:00] => Array
        (
            [17:30] => 3
        )

    [2015-07-22 13:00@#@2015-07-22 14:00] => Array
        (
            [17:30] => 1
        )

    [2015-07-22 14:00@#@2015-07-22 15:00] => Array
        (
            [17:30] => 0
        )
)

我想添加第1和第2个元素添加元素应该添加,然后第2和第3个应该同样添加等等。 我想要像这样的数组

 Array
    (
    [2015-07-22 09:00@#@2015-07-22 10:00] => Array
            (
                [17:30] => 1
            )

        [2015-07-22 10:00@#@2015-07-22 11:00] => Array
            (
                [17:30] => 3
            )

        [2015-07-22 11:00@#@2015-07-22 12:00] => Array
            (
                [17:30] => 11
            )

        [2015-07-22 12:00@#@2015-07-22 13:00] => Array
            (
                [17:30] => 14
            )

        [2015-07-22 13:00@#@2015-07-22 14:00] => Array
            (
                [17:30] => 15
            )

        [2015-07-22 14:00@#@2015-07-22 15:00] => Array
            (
                [17:30] => 15
            )
    )

我通过使用php的下一个和上一个功能尝试了很多,但它不能像我想的那样为关联数组工作。任何人都可以帮助我得到像这样的数组。

2 个答案:

答案 0 :(得分:1)

出于某种原因,这使我的好奇心达到了顶峰。这可能不是实现结果的最有效记忆的方式,但我现在就开始工作了!

<?php

 $toTransform                     =   array
 (
   "2015-07-22 09:00@#@2015-07-22 10:00" => array("17:30" => 1),
   "2015-07-22 10:00@#@2015-07-22 11:00" => array("17:30" => 2),
   "2015-07-22 11:00@#@2015-07-22 12:00" => array("17:30" => 8),
   "2015-07-22 12:00@#@2015-07-22 13:00" => array("17:30" => 3),
   "2015-07-22 13:00@#@2015-07-22 14:00" => array("17:30" => 1),
   "2015-07-22 14:00@#@2015-07-22 15:00" => array("17:30" => 0),
 );

 $expectedArray                   =   array(
   "2015-07-22 09:00@#@2015-07-22 10:00"   =>  array("17:30" =>1),
   "2015-07-22 10:00@#@2015-07-22 11:00"   =>  array("17:30" =>3),
   "2015-07-22 11:00@#@2015-07-22 12:00"   =>  array("17:30" =>11),
   "2015-07-22 12:00@#@2015-07-22 13:00"   =>  array("17:30" =>14),
   "2015-07-22 13:00@#@2015-07-22 14:00"   =>  array("17:30" =>15),
   "2015-07-22 14:00@#@2015-07-22 15:00"   =>  array("17:30" =>15),
 );

 /**
  * @param array $array
  * @return array
  */
 function sumPreviousAndCurrent($array) {
   // the result we'll return at the end
   $result                        = array();
   $previousValue                 = 0;
   // loop through adding each element to the array
   foreach ($array as $k => $v) {
       // what is the first value in this array?
       $thisValue                 =   reset($v);
       // find out what the key of this value is
       $firstKey                  =   key($v);
       // figure out what the new value will be
       $newValue                  =   $thisValue + $previousValue;
       // set the value of the first element of this array as the new value
       $v[$firstKey]             =   $newValue;
       // set $previousValue to $newValue for the next pass
       $previousValue             =   $newValue;
       $result[$k]                = $v;
   }

   return $result;
 }

 // run the tranformation
 $result                          = sumPreviousAndCurrent($toTransform);

 // did it work?
 if ($result === $expectedArray) {
   echo 'Successfully transformed the array.';
 }
 else {
   echo 'Test failed.';
   ?>
   <strong>Input:</strong>
   <pre>
     <?php print_r($toTransform); ?>
   </pre>
   <strong>Expected Result:</strong>
   <pre>
     <?php print_r($expectedArray); ?>
   </pre>
   <strong>Output:</strong>
   <pre>
     <?php print_r($result); ?>
   </pre>
   <?php
 }

答案 1 :(得分:0)

使用来自@DomWolden的数据作为我的测试数据。

到目前为止保持值的运行总数可能更有效...

Working example at eval.in

代码:

gitk

显示输出:

 $prevRowTotal = 0; // accumulate current row values
 $outArray = array(); // output array in here

 foreach ($toTransform as $key => $valueArray) {
        $prevRowTotal += current($valueArray);
        $outArray[$key] = array(key($valueArray) => $prevRowTotal);
 };