我有两个数组。现在我想比较两个数组中的一些元素,基于我需要将一个数组元素填充到新数组中。
Array1: $days this is an array of elements between two dates.
$days = Array
(
[0] => 2012-06-23
[1] => 2012-06-24
[2] => 2012-06-25
[3] => 2012-06-26
[4] => 2012-06-27
[5] => 2012-06-28
[6] => 2012-06-29
[7] => 2012-06-30
[8] => 2012-07-01
[9] => 2012-07-02
[10] => 2012-07-03
[11] => 2012-07-04
[12] => 2012-07-05
[13] => 2012-07-06
[14] => 2012-07-07
[15] => 2012-07-08
[16] => 2012-07-09
[17] => 2012-07-10
[18] => 2012-07-11
[19] => 2012-07-12
[20] => 2012-07-13
[21] => 2012-07-14
[22] => 2012-07-15
[23] => 2012-07-16
)
Array2: $summary is the array which i am getting from the database..
$summary = Array
(
[0] => Array
(
[date] => 2012-06-23
[no_of_posts] => 1
)
[1] => Array
(
[date] => 2012-06-24
[no_of_posts] => 2
)
[2] => Array
(
[date] => 2012-06-25
[no_of_posts] => 1
)
[3] => Array
(
[date] => 2012-06-26
[no_of_posts] => 1
)
[4] => Array
(
[date] => 2012-06-27
[no_of_posts] => 1
)
现在从这两个数组中,我需要一个数组,它告诉我在这个日期有很多次帖子,如果没有针对某个日期的帖子,那么它应该为零。我试过这样......
$result = array();
foreach($summary as $key=>$s) {
foreach($days as $d) {
if($s['date'] == $d) {
$result[$d] = $s['no_of_posts'];
} else {
$result[$d] = 0;
}
}
}
我知道我找不到错误…………任何想法。
输出数组需要如下所示.....
$result = Array
Array
(
[2012-06-23] = 1
[2012-06-24] = 2
[2012-06-25] = 1
[2012-06-26] = 1
[2012-06-27] = 1
[2012-06-28] = 0
[2012-06-29] = 0
[2012-06-30] = 0
[2012-07-01] = 0
[2012-07-02] = 0
[2012-07-03] = 0
[2012-07-04] = 0
[2012-07-05] = 0
[2012-07-06] = 0
[2012-07-07] = 0
[2012-07-08] = 0
[2012-07-09] = 0
[2012-07-10] = 0
[2012-07-11] = 0
[2012-07-12] = 0
[2012-07-13] = 0
[2012-07-14] = 0
[2012-07-15] = 0
[2012-07-16] = 0
)