2013-07-17 96 views
0

我有一個獎項列表,我希望按年份進行分組和列表。我可以列出這些項目,但是在嵌套它們時卻不成功。如何從數組中創建嵌套列表PHP

///// ARRAY

Array 
(
[0] => Array 
    (
     [Award] => Array 
      (
       [id] => 5 
       [order] => 4 
       [publish] => 1 
       [year] => 2015 
       [title] => Test Award #5 
       [copy] => Duis aute irure dolor in reprehenderit. 
       [slug] => test-award-5 
      ) 

    ) 

[1] => Array 
    (
     [Award] => Array 
      (
       [id] => 4 
       [order] => 3 
       [publish] => 1 
       [year] => 2014 
       [title] => Test Award #4 
       [copy] => Duis aute irure dolor in reprehenderit. 
       [slug] => test-award-4 
      ) 

    ) 

[2] => Array 
    (
     [Award] => Array 
      (
       [id] => 1 
       [order] => 0 
       [publish] => 1 
       [year] => 2013 
       [title] => Test Award #1 
       [copy] => Duis aute irure dolor in reprehenderit. 
       [slug] => test-award-1 
      ) 

    ) 

[3] => Array 
    (
     [Award] => Array 
      (
       [id] => 2 
       [order] => 1 
       [publish] => 1 
       [year] => 2013 
       [title] => Test Award #2 
       [copy] => Duis aute irure dolor in reprehenderit. 
       [slug] => test-award-2 
      ) 

    ) 

) 

////控制器

class AwardsRecognitionController extends AppController { 

var $name = 'Award'; 

/***** 
    PUBLIC 
*****/ 
function index(){ 

    $awards=$this->Award->find('all', array(
      'conditions'=>array('publish'=>1), 
      'order'=>array('Award.year DESC') 
     )); 

     $this->set('awards', $awards); 

} 

//// VIEW

<div id="award_container"> 

<?php 
    echo "<ul>"; 

    foreach($awards as $award){ 
     echo " 

      <li class='award' style='color:black;'> 

       <strong>".$award['Award']['year']."</strong> 

       <span class='award_title'>".$award['Award']['title']."</span> 

       <p>".$award['Award']['copy']."</p> 
      </li> 

     "; 
    } 
?> 
</div> 

我所試圖做的是輸出數據轉換爲像這樣的嵌套列表。

2015 
    Test Award #5 
2014 
    Test Award #4 
2013 
    Test Award #2 
    Test Award #1 

歡迎任何幫助!謝謝!

回答

0
$array = [your existing array]; 

$n = array(); 
foreach($array as $a) { 
    $year = $a['Award']['year']; 
    if(!isset($n[$year])) { $n[$year] = array(); } 
    $n[$year][] = $a['Award']; 
} 

// now you have a new array $n looking like this 

print_r($n); 

ksort($n); // sort array by key 

// and you can walk through it 

foreach($n as $year => $awards) { 
    echo $year."<br>"; 
    foreach($awards as $aw) { 
     echo $aw['title']."<br>"; 
    } 
} 
+0

史蒂芬,太感謝你了!,是完全做到了。 – Sebas

0
$new = array(); 
foreach ($awards as $key => $award){ 
    $new[$award['Award']['year']][] = $award['Award']; 
} 
var_dump($new); 

輸出:

Array(
    [2015] => Array(
      [0] => Array(
        [id] => 5 
        [order] => 4 
        [publish] => 1 
        [year] => 2015 
        [title] => Test Award #5 
        [copy] => Duis aute irure dolor in reprehenderit. 
        [slug] => test-award-5 
       ) 
     ) 
    [2014] => Array(
      [0] => Array(
        [id] => 4 
        [order] => 3 
        [publish] => 1 
        [year] => 2014 
        [title] => Test Award #4 
        [copy] => Duis aute irure dolor in reprehenderit. 
        [slug] => test-award-4 
       ) 
     ) 
    [2013] => Array(
      [0] => Array(
        [id] => 1 
        [order] => 0 
        [publish] => 1 
        [year] => 2013 
        [title] => Test Award #1 
        [copy] => Duis aute irure dolor in reprehenderit. 
        [slug] => test-award-1 
       ) 
      [1] => Array(
        [id] => 2 
        [order] => 1 
        [publish] => 1 
        [year] => 2013 
        [title] => Test Award #2 
        [copy] => Duis aute irure dolor in reprehenderit. 
        [slug] => test-award-2 
       ) 
     ) 
)