PHP数组:使用匹配日期求和值
我想弄清楚,如果多维数组的某些值有相似的日期,如何求和 这是我的阵列:PHP数组:使用匹配日期求和值,php,arrays,Php,Arrays,我想弄清楚,如果多维数组的某些值有相似的日期,如何求和 这是我的阵列: <?$myArray=array( array( 'year' => 2011, 'month ' => 5, 'day' => 13, 'value' => 2 ), array( 'year' => 2011, 'month '=> 5, 'day' =>
<?$myArray=array(
array(
'year' => 2011,
'month ' => 5,
'day' => 13,
'value' => 2
),
array(
'year' => 2011,
'month '=> 5,
'day' => 14,
'value' => 5
),
array(
'year' => 2011,
'month ' => 5,
'day' => 13,
'value' => 1
),
array(
'year' => 2011,
'month ' => 5,
'day' => 14,
'value' => 9
)
);?>
以下是我希望输出的外观:
<?$output=array(
array(
'year' => 2011,
'month ' => 5,
'day' => 13,
'value' => 3 //the sum of 1+2
),
array(
'year' => 2011,
'month '=> 5,
'day' => 14,
'value' => 14 //the sum of 5+9
)
);?>
注意4个子数组是如何在年/月/日
上匹配的,然后只对值
求和。我见过其他的,但是找不到一个只有值
被求和,而不是年/月/日
值被求和的
想法?最初,使用年/月/日组合为输出数组编制索引可能是最简单的: 注意:上面的示例数组的所有
month
键都带有尾随空格。我只是在这里使用month
,没有尾随空格
// Initialize output array...
$out = array();
// Looping over each input array item
foreach ($myArray as $elem) {
// Initialize a new element in the output keyed as yyyy-mm-dd if it doesn't already exist
if (!isset($out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']])) {
$out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']] = array(
// Set the date keys...
'year' => $elem['year'],
'month' => $elem['month '],
'day' => $elem['day'],
// With the current value...
'value' => $elem['value']
);
}
// If it already exists, just add the current value onto it...
else {
$out[$elem['year'] . "-" . $elem['month '] . "-" . $elem['day']]['value'] += $elem['value'];
}
}
// Now your output array is keyed by date. Use array_values() to strip off those keys if it matters:
$out = array_values($out);
输出(在调用数组\u值()
之前):
更新:
要对单键日期(而不是3个部分)执行相同的操作,无需串联即可轻松完成:
$myArray=array(
array(
'date' => '2011-05-13',
'value' => 2
),
array(
'date' => '2011-05-14',
'value' => 5
),
array(
'date' => '2011-05-13',
'value' => 7
),
array(
'date' => '2011-05-14',
'value' => 3
),
);
foreach ($myArray as $elem) {
// Initialize a new element in the output if it doesn't already exist
if (!isset($out[$elem['date']])) {
$out[$elem['date'] = array(
// Set the date keys...
'date' => $elem['date'],
// With the current value...
'value' => $elem['value']
);
}
else {
$out[$elem['date']]['value'] += $elem['value'];
}
}
我会这样做的。结果将出现在
$newArray
中,日期时间对象作为键。如果您只想将其作为索引数组,那么应该很容易做到
// Example array
$myArray = array(
array(
'date' => new DateTime('1993-08-11'),
'value' => 3
),
array(
'date' => new DateTime('1993-08-11'),
'value' => 5
)
);
$newArray = array();
foreach($myArray as $element)
{
$iterationValue = $element['value'];
$iterationDate = $element['date'];
$dateKey = $iterationDate->format('Y-m-d');
if(array_key_exists($dateKey, $newArray))
{
// If we've already added this date to the new array, add the value
$newArray[$dateKey]['value'] += $iterationValue;
}
else
{
// Otherwise create a new element with datetimeobject as key
$newArray[$dateKey]['date'] = $iterationDate;
$newArray[$dateKey]['value'] = $iterationValue;
}
}
nl2br(print_r($newArray));
实际上,最终做了与@MichaelBerkowski解决方案几乎相同的事情。尽管如此,当您不想在应用程序的后面处理日期时,拥有DateTime对象总是更加灵活
编辑:现在对其进行了测试并修复了错误如果您只使用DateTime对象,它会简单得多。@MrAzulay谢谢您能给我显示/发送一个链接,了解使用DateTime对象时数组匹配/求和的效果吗?我将更新我的问题,以包括
$myArray
-@Michael的这种可能的替代格式,感谢@MrAzulay的建议,如果这样做更简单的话,我可以将日期重新格式化为yyyyy-mm-dd
(子数组只包含2个而不是4个键值对)。@dainispools我修复了双[[
括号已存在。复制/粘贴错误我测试了您的代码,并得到以下分析错误:语法错误,在第40行的C:\data\test.php中出现意外的“[”,应为“]”。这是在$out[[$elem['year'].-“$elem['month'.-”$elem['day']]=array(
@timperson那么同样的模式也可以,只是不要使用所有复杂的连接来构建数组键。只需$out[$elem['date']]
@MichaelBerkowski好的,我不太明白,看看这个要点:。我想保留$myArray数组结构,但这段代码没有。在$out
数组中应该只有两个子数组。-@MrAzulay,谢谢你的解决方案,格式()
没有在什么地方声明吗?@Timperson你到底是什么意思?格式()是datetime中将日期转换为字符串的方法。我的第一个意图是使用对象作为键,但我忘记了这在PHP中是不可能的。因此我必须将其设置为字符串。哦,对不起,错过了新日期时间('xxxx-xx-xx')
声明,以为它只是xxxx-xx
。谢谢
// Example array
$myArray = array(
array(
'date' => new DateTime('1993-08-11'),
'value' => 3
),
array(
'date' => new DateTime('1993-08-11'),
'value' => 5
)
);
$newArray = array();
foreach($myArray as $element)
{
$iterationValue = $element['value'];
$iterationDate = $element['date'];
$dateKey = $iterationDate->format('Y-m-d');
if(array_key_exists($dateKey, $newArray))
{
// If we've already added this date to the new array, add the value
$newArray[$dateKey]['value'] += $iterationValue;
}
else
{
// Otherwise create a new element with datetimeobject as key
$newArray[$dateKey]['date'] = $iterationDate;
$newArray[$dateKey]['value'] = $iterationValue;
}
}
nl2br(print_r($newArray));