1

私は2つの配列を持っています。ここで、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 に答える