需要数组格式化帮助?

时间:2013-01-06 14:57:54

标签: php arrays codeigniter

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

 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
        )

3 个答案:

答案 0 :(得分: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数组现在应该是您所追求的。我相信。

答案 1 :(得分: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);

答案 2 :(得分:-1)

代码应为: $ days =数组         (             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',             );         $ summary = array         (             0 =>排列             (                 '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;
            }
        }
    }