How to creat a nested list from an array PHP -
i have list of awards i'd group , list year. i'm able list items, have been unsuccessful in nesting them.
/////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 ) ) )
////controller
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 $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>
what trying output data nested list this.
2015 test award #5 2014 test award #4 2013 test award #2 test award #1
any welcomed! thanks!
$array = [your existing array]; $n = array(); foreach($array $a) { $year = $a['award']['year']; if(!isset($n[$year])) { $n[$year] = array(); } $n[$year][] = $a['award']; } // have new array $n looking print_r($n); ksort($n); // sort array key // , can walk through foreach($n $year => $awards) { echo $year."<br>"; foreach($awards $aw) { echo $aw['title']."<br>"; } }
Comments
Post a Comment