0

这似乎很容易,但无法弄清楚如何做到这一点。当前数组数据按日期列出,按日期数量列出,所以我需要将所有日期组合起来:日、月、6 个月、1 年。我需要安排数组数据看起来像下面的第二个数组。


    array
      0 => enter code here
        array
          '1_day' => int 37
      1 => 
        array
          '30_day' => int 3275
      2 => 
        array
          '180_day' => int 3908
      3 => 
        array
          '1_year' => int 6933
      4 => 
        array
          'date' => string '2013-02-13' (length=10)
      5 => 
        array
          '1_day' => int 46
      6 => 
        array
          '30_day' => int 3134
      7 => 
        array
          '180_day' => int 3764
      8 => 
        array
          '1_year' => int 6820
      9 => 
        array
          'date' => string '2013-02-12' (length=10)
      10 => 
        array
          '1_day' => int 61
      11 => 
        array
          '30_day' => int 3127
      12 => 
        array
          '180_day' => int 3750
      13 => 
        array
          '1_year' => int 6807
      14 => 
        array
          'date' => string '2013-02-11' (length=10)


    array
      0 => 
          '1_day' => int 37
          '30_day' => int 3275
          '180_day' => int 3908
          '1_year' => int 6933
          'date' => string '2013-02-13' (length=10)
      1 => 
          '1_day' => int 46
          '30_day' => int 3134
          '180_day' => int 3764
          '1_year' => int 6820
          'date' => string '2013-02-12' (length=10)
      2 => 
          '1_day' => int 61
          '30_day' => int 3127
          '180_day' => int 3750
          '1_year' => int 6807
          'date' => string '2013-02-11' (length=10)

4

3 回答 3

0
Try this one....

$result = array();     
 $i = 0;
foreach($data as $key => $d) 
{
    $result[$i]['1_day'] = $data[$key]['1_day'];
    $result[$i]['30_day'] = $data[$key+1]['30_day'];
    $result[$i]['180_day'] = $data[$key+2]['180_day'];
    $result[$i]['1_year'] = $data[$key+3]['1_year'];
    $result[$i]['date'] = $data[$key+4]['date'];
    $key = $key+5;
    $i++;
  }
于 2013-02-13T14:40:55.117 回答
0

未经测试,但这应该让你接近:

<?php

$result = array();

for ($i = 0; $i <= count($array); $i++)
{
    if ($array[$i] == 'date')
    {
        $result[] = array(
            '1_day'    => $array[$i-4],
            '30_day'   => $array[$i-3],
            '180_day'  => $array[$i-2],
            '1_year'   => $array[$i-1],
            'date'     => $array[$i],
        );
    }
}
于 2013-02-13T14:44:42.633 回答
0

另一种方法:

<?php

$count = count($inArray);

//In place subarray merger, by this you don't have to modify keys every time they change.
//Constraint: A sub-array at every index.

for ($idx =0, $jdx =0; $idx < $count; $idx +=5, $jdx++) {
    $inArray[$jdx] = array_merge($inArray[$idx],
                                $inArray[$idx +1],
                                $inArray[$idx +2],
                                $inArray[$idx +3],
                                $inArray[$idx +4]);
}

array_splice ($inArray, $count/5);

/* OR into another array */

for ($idx =0, $jdx =0; $idx < $count; $idx +=5, $jdx++) {
    $newArray[$jdx] = array_merge($inArray[$idx],
                                $inArray[$idx +1],
                                $inArray[$idx +2],
                                $inArray[$idx +3],
                                $inArray[$idx +4]);
}

?>
于 2013-02-13T15:08:01.087 回答