2014-02-28 84 views
0

分類或組陣列我有一個按日期倒序這樣的分組歷史列表:如何通過日期

-----------  
28/Feb/2014  
----------- 
27/Feb/2014 
----------- 
27/Feb/2014 
----------- 
27/Feb/2014 
------------  
26/Feb/2014 
------------ 
26/Feb/2014 

我要打印這樣的。

-----------  
28/Feb/2014  
----------- 
27/Feb/2014  
27/Feb/2014 
27/Feb/2014 
------------  
26/Feb/2014 
26/Feb/2014 

這裏是我的數組:

array(
    (int) 0 => array(
     'BriefHistory' => array(
      'id' => '2', 
      'created' => '2014-02-28 14:51:08', 
      'created_by' => '7827', 
      'order_id' => 'OBE10003', 
      'brief_instalment_id' => '2', 
      'brief_history_type_id' => '1' 
     ) 
    ), 
    (int) 1 => array(
     'BriefHistory' => array(
      'id' => '4', 
      'created' => '2014-02-27 16:18:40', 
      'created_by' => '7827', 
      'order_id' => 'OBE10003', 
      'brief_instalment_id' => '2', 
      'brief_history_type_id' => '4' 
     ) 
    ), 
    (int) 2 => array(
     'BriefHistory' => array(
      'id' => '1', 
      'created' => '2014-02-27 14:51:08', 
      'created_by' => '7827', 
      'order_id' => 'OBE10003', 
      'brief_instalment_id' => '1', 
      'brief_history_type_id' => '1' 
     ) 
    ), 
    (int) 3 => array(
     'BriefHistory' => array(
      'id' => '3', 
      'created' => '2014-02-26 16:18:09', 
      'created_by' => '7827', 
      'order_id' => 'OBE10003', 
      'brief_instalment_id' => '1', 
      'brief_history_type_id' => '3' 
     ) 
    ) 
) 

PHP:

<?php foreach ($briefHistories as $briefHistorie) { ?>       
     <div class="brief-summary-row"> 
      <div> 
       <?php echo TimeUtil::format("d/M/Y", $briefHistorie['BriefHistory']['created']); ?> 
      </div> 
    </div> 
<?php } ?> 

回答

0

他人

$arrayDates = array(); 
    $i = 0; 
    $key = array(); 
    foreach ($briefHistories as $briefHistorie) { 
     $key_name = date('Ymd', strtotime($briefHistorie['BriefHistory']['created'])); 
     if(!isset($key[$key_name])){ 
      $key[$key_name]=$i; 
      $i++; 
     } 
     $arrayDates[$key[$key_name]][]= $briefHistorie; 
    } 



    foreach ($arrayDates as $arrayDate) { 
      echo '<div class="brief-summary-row">'; 
       foreach ($arrayDate as $data) { 
        echo '<strong>'.date('d-m-Y', strtotime($data['BriefHistory']['created'])).'</strong>';     
       }   
      echo '</div>'; 
    } 

打印出來=>

<div class="brief-summary-row"> 
    <strong>28-02-2014</strong> 
</div> 
<div class="brief-summary-row"> 
    <strong>27-02-2014</strong> 
    <strong>27-02-2014</strong> 
    <strong>27-02-2014</strong> 
    <strong>27-02-2014</strong> 
</div> 
<div class="brief-summary-row"> 
    <strong>26-02-2014</strong> 
    <strong>26-02-2014</strong> 
</div> 
<div class="brief-summary-row"> 
    <strong>25-02-2014</strong> 
</div> 

其他Here is array demo

0
<?php 
    $current = TimeUtil::format("d/M/Y", $briefHistories[0]['BriefHistory']['created']); 
    echo '<div class="brief-summary-row"><div>'; 
    foreach ($briefHistories as $briefHistorie) {  
      $created = TimeUtil::format("d/M/Y", $briefHistorie['BriefHistory'['created']); 
      echo $created; 
      if($created != $current){ 
       echo '</div></div><div class="brief-summary-row"><div>'; 
       $current = $created; 
      } 
    } 
    echo '</div></div>'; 
?> 
+0

對不起這不起作用 – Fury

0

東西沿着這些路線。可壓實:

$previous = ''; 

foreach ($briefHistories as $briefHistorie) { 
    $date = TimeUtil::format("d/M/Y", $briefHistorie['BriefHistory']['created']); 

    if($date != $previous) { 
     echo '<div class="brief-summary-row">'; 
    } 

    echo '<div>'.$date.'</div>'; 

    if($date != $previous) { 
     echo '</div>'; 
    } 
    $previous = $date; 
} 
+0

謝謝。它是在附近,但仍然有相同的日期 – Fury

+0

@伊薩克Rajaei,你能舉一個例子,這是不正確的? – cornelb

+0

第一個27/2月在div中顯示,但其他的在div外 – Fury