-1

Sorry about the bad title.

First off I'm pulling a MySQL date and news articles attached to the date I have the information in an associative array.

My MySQL query is:

select year(news_date) as a year,
 month(news_date) as the month,
 news_content as an article
 from news
 order by year desc, month asc

So how do I order it in PHP from an associative array so that its grouped by year (all 2016 articles are grouped by month, all 2015 articles are grouped by month, etc.)

I'm a trainee developer and my tutor isn't here otherwise I would have asked him :)

GiuServ
  • 1,215
  • 1
  • 13
  • 33

1 Answers1

0

You just iterate all of them:

$yearGroup = array();
foreach($array as $key => $value){
    $yearGroup[$value['year']] = $value;
}

and in the end you'll have an associative array base on year.

I'm not sure if this is the info you need you should provide a code sample with what you have and what you want to achieve.

obsergiu
  • 350
  • 3
  • 13
  • `while ($all1 = $db->fetch_assoc($all)) { $month = newsNavigation::changeMonths($all1["month"]); echo $all1['year'] . ', ' . $month . ', ' . $all1["count"] . '
    '; }` was what i was using but it just gives me a list of dates and counts of articles in the month of that year I wanted all dates e.g. 2016, January, 2016, January, 2016, January, 2016, Febuary, how do I make it so I can seperate the years so it's like 2016 - feburary - 5 - march - 6 if you understand what I'm saying I'm not great at explaining things
    – user2877018 Nov 09 '16 at 15:12
  • I kinda want it like 2016 as its on array with months with in it and then articles within each month's as an array – user2877018 Nov 09 '16 at 15:19