一定条件下的关联数组元素求和
Summation of associative array element with certain condition
我在 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 的 next 和 previous 函数尝试了很多,但它不能像我想要的那样用于关联数组。任何人都可以帮助我获得这样的数组。
出于某种原因,这激起了我的好奇心。这可能不是实现结果的最有效内存方式,但我现在要开始工作了!
<?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
}
使用@DomWolden 回答的数据作为我的测试数据。
到目前为止,保持 运行 个总值可能更有效...
代码:
$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);
};
显示输出:
var_dump($outArray);
// will throw execption if different
assert($outArray == $expectedArray);
我在 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 的 next 和 previous 函数尝试了很多,但它不能像我想要的那样用于关联数组。任何人都可以帮助我获得这样的数组。
出于某种原因,这激起了我的好奇心。这可能不是实现结果的最有效内存方式,但我现在要开始工作了!
<?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
}
使用@DomWolden 回答的数据作为我的测试数据。
到目前为止,保持 运行 个总值可能更有效...
代码:
$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);
};
显示输出:
var_dump($outArray);
// will throw execption if different
assert($outArray == $expectedArray);