Question Regarding Building an Array

Hi,

I’m trying to create a news archive which will display the year and then the posts associated with that year underneath.

My question is how do I insert an array within an array. For example first I find out the years and build that into the array and then I want to loop through the posts and put that into the array associated with the year.

Function code is below.

Thanks

	public function displayNewsArchive() {
		$output['rows'] = array();
		
		$db = db::getInstance();
		if ($result = $db->query("SELECT DISTINCT `year` FROM `news` WHERE `news_show_live`= 1 ORDER BY `year` ASC")) {
			if ($rows = $db->getrows($result)) {
				for ($i=0; $i<count($rows); $i++) {
					//build array and add year first
					$output['rows'][$i] = array (
						'year'=>$rows[$i]['year'] array (
							//query database to find news post for each year
							if ($result = $db->query("SELECT * FROM `news` WHERE `year`=".intval($rows[$i]['year'])." AND `news_show_live`=1  ORDER BY `news_date_published` ASC")) {
								if ($rows = $db->getrows($result)) {
									//loop through each news post and build insert into array
									for ($i=0; $i<count($rows); $i++) {
									'value'=>$rows[$i]
								}
							}
						);		
					);		
				}
			}
			
		  }
			
		return $output;
	}

Don’t do multiple queries inside a loop. You can do it all with one query (ordered by year and news_date_published), and then loop through the result. To display the year only once, all you need is to keep track of changing years while looping.