好的 - 所以这个应该很容易。我有两个数组。数组一具有如下结构:
[
[
'value' => 3337
'end_time' => '2012-10-07T07:00:00+0000'
],
[
'value' => 2811
'end_time' => '2012-10-09T07:00:00+0000'
],
[
'value' => 1318
'end_time' => '2012-10-14T07:00:00+0000'
]
和数组2:
[
[
'_id' => '2012-10-07'
'value' => 50
],
[
'_id' => '2012-10-09'
'value' => 15
],
[
'_id' => '2012-10-10'
'value' => 2
]
]
SO....我想根据 array1[_id] = substr(array2['end_time'], 0, 10) 将数组 1 的元素除以数组 2 的元素。
数组 2 没有数组 1 中所有 id 的结束时间值。
生成的数组应如下所示:
[
'2012-10-01' => 0.11
'2012-10-02' => 0 (if this date is not available in the second array).
'2012-10-03' => 0.12312
]
这样做:
$kermit = array();
foreach($dauresult['data']['0']['values'] as $subdau) {
foreach($revenue['results'] as $subrev) {
$date = substr($subdau['end_time'], 0, 10);
if($date == $subrev['_id']) {
$kermit[$date] = $subrev['value']/$subdau['value'];
}
}
}
我可以得到一个如下所示的数组:
Array
(
[2012-09-30] => 0.0044950554390171
[2012-10-01] => 0.019565990750623
[2012-10-05] => 0.015487397509869
[2012-10-07] => 0.020177562550444
[2012-10-09] => 0.0075150300601202
[2012-10-10] => 0.00095831336847149
[2012-10-11] => 0.0010183299389002
[2012-10-12] => 0.0010126582278481
[2012-10-13] => 0.029866666666667
[2012-10-14] => 0.029779630732579
[2012-10-15] => 0.011926058437686
[2012-10-18] => 0.0018844221105528
[2012-10-19] => 0.0005941770647653
[2012-10-21] => 0.0023781212841855
[2012-10-27] => 0.0011820330969267
[2012-10-28] => 0.0011467889908257
)
但我不能做的是让缺失的日期(10-20、10-16 等)显示为 0。愚蠢的问题,但这让我有点发疯。有什么建议么?谢谢!-蕨类