1

我有两个数组。现在我想比较两个数组中的一些元素,基于我需要将一个数组元素填充到新数组中。

 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
        )
4

3 回答 3

1

最快的方法是遍历数据库结果并创建一个按日期索引的值数组:

$summaryTotals = array();
foreach($summary as $dbSummary) {
    $summaryTotals[$dbSummary['date']] = $dbSummary['no_of_posts'];
}

然后遍历您的日期数组,输入摘要数组中匹配的值,如下所示:

$dayValues = array();
foreach($days as $day) {
    if(isset($summaryTotals[$day])) {
        $dayValues[$day] = $summaryTotals[$day];
    } else {
        $dayValues[$day] = 0;
    }
}

dayValues 数组现在应该是您所追求的。我相信。

于 2013-01-06T15:05:40.000 回答
0

试试这个方法:

   $newArr = array();

    foreach($summary as $key=>$s) {
      $newArr[$s['date']] = $s['no_of_posts'];
    }

    $newDays = array_fill_keys(array_values($days),'0');

    $newData = $newDays + $newArr;

    print_r ($newData);
于 2013-01-06T15:08:30.213 回答
-1

19 => '2012-07-12', 20 => '2012-07-13', 21 => '2012-07-14', 22 => '2012-07-15', 23 => '2012- 07-16', ); $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 if(!isset($result[$d])){
                $result[$d] = 0;
            }
        }
    }
于 2013-01-06T15:26:22.763 回答