2015-05-09 58 views
2

我想根據月份來隔離數組。基於月的隔離數組

這裏是一個陣列的樣品和輸出我希望得到:

Array 
{ 
    [1] => 1:"Fruit"; 
    [2] => 2:"Mangoe"; 
    [3] => 3:"2015-2-5"; 
} 

Array 
{ 
    [1] => 1:"Fruit"; 
    [2] => 2:"Banana"; 
    [3] => 3:"2015-4-11"; 
} 

Array 
{ 
    [1] => 1:"Fruit"; 
    [2] => 2:"Orange"; 
    [3] => 3:"2015-2-10"; 
} 

Array 
{ 
    [1] => 1:"Fruit"; 
    [2] => 2:"Pineapple"; 
    [3] => 3:"2015-8-3"; 
} 

目標產出:

February 
Fruit:Banana 
Fruit:Orange 

April 
Fruit:Banana 

August 
Fruit:Pineapple 

這是我的代碼:

<?php 

    foreach ($results as $key => $value) 
    { 
     $value = get_object_vars($value); 
     $raw_slice = explode('s:', $value['rawdata']); 


     //VARIABLES 
     //GET TITLE 
     $title_cut = explode(':"', str_replace('";', '', $raw_slice[2])); 
     $title = $title_cut['1']; 

     //GET DATE 
     $date_cut = explode(':"', str_replace('";', '', $raw_slice[3])); 
     $date_text = $date_cut['1']; 



     $month = "01"; 
     $year = "2015"; 

     $start_date = "01-".$month."-".$year; 
     $start_time = strtotime($start_date); 

     $end_time = strtotime("+1 month", $start_time); 

     for($i=$start_time; $i<$end_time; $i+=86400) 
     { 
      $list[] = date('Y-m-d-D', $i); 
     } 


     echo "<pre>"; 

     echo "<div class ='title'>"; 
     echo $title.'<br>'; 
     echo "</div>"; 

     echo "<div class ='date'>"; 
     echo date('F \ j,\ Y',strtotime($date_text)); 
     echo "</div>"; 


     echo "</pre>";   
    } 

?> 
+0

哪兒芒果在你預期的輸出? –

+2

這個數組是如何創建的?好像你可以讓你的生活變得更輕鬆,如果你創建了不同的數組。 – Devon

回答

0

這是一個奇怪的陣列,如果可能的話,你會很好地重組它。不知道它是如何構建的。但是,如果這就是你有工作,這裏是得到你想要輸出的一種方法:

$arrays = array 
(
    array 
    (
     1 => '1:"Fruit"', 
     2 => '2:"Mangoe"', 
     3 => '3:"2015-2-5"', 
    ), 
    array 
    (
     1 => '1:"Fruit"', 
     2 => '2:"Banana"', 
     3 => '3:"2015-4-11"', 
    ), 
    array 
    (
     1 => '1:"Fruit"', 
     2 => '2:"Orange"', 
     3 => '3:"2015-2-10"', 
    ), 
    array 
    (
     1 => '1:"Fruit"', 
     2 => '2:"Pineapple"', 
     3 => '3:"2015-8-3"', 
    ), 
); 
$months = array(); 
foreach($arrays as $a) 
{ 
    preg_match('/\"([^\"]*?)\"/', $a[1], $category); 
    preg_match('/\"([^\"]*?)\"/', $a[2], $item); 
    preg_match('/\"([^\"]*?)\"/', $a[3], $date); 
    list($Y,$M,$D) = explode('-', str_replace('"', '', $date[0])); 
    $M = str_pad($M, 2, 0, STR_PAD_LEFT); 
    $D = str_pad($D, 2, 0, STR_PAD_LEFT); 
    $unix = strtotime($Y.'-'.$M.'-'.$D); 
    $key = date('m', $unix); 
    $months[$key]['month'] = date('F', $unix); 
    $months[$key]['categories'][str_replace('"', '', $category[0])][] = str_replace('"', '', $item[0]); 
} 
ksort($months); 
echo '<pre>'; 
foreach($months as $m) 
{ 
    echo "<div class ='month'>"; 
    echo $m['month']; 
    echo "</div>"; 
    ksort($m['categories']); 
    foreach($m['categories'] as $cat => $items) 
    { 
     foreach($items as $item)   
     { 
      echo "<div class ='item'>"; 
      echo $cat.':'.$item; 
      echo "</div>"; 
     } 
    } 
    echo '<br>'; 
} 
echo '</pre>';