2

I basically have this code which determines the number of same dates in an array:

function get_archives_html ($blog) {
    //tag array to store arrays with same tag
    $count_tags = array();


    //store output
    $output = '';

    foreach($blog as $id) {
        //abbreviate date name
        $originalDate = $id["date"]["year"].'-'.$id["date"]["month"].'-'.$id["date"]["day"];
        $abbrevDate = date("F, Y", strtotime($originalDate));

        if(!isset($count_tags[$abbrevDate])) {
            $count_tags[$abbrevDate] = 0;
        }

        ++$count_tags[$abbrevDate];
    }

    // Sort your tags from hi to low
    //arsort($count_tags);
    var_dump($count_tags);




    foreach($count_tags as $month=>$id) {
        $output.= '<p><a href="#">'. $month.($id > 1 ? ' ('.$id .')' : '').'</a></p>';
    }

    return $output;
}

The output would look like this:

 $arr = array(
        "November, 2016" => "2",
        "October, 2016" => "5",
        "October, 2017" => "3",
        "September, 2017" => "6" 
    );

Now, I use the keys to display it on the html. Problem is that it is not arranged properly by date but rather by alphabet.

So, my question is, how can I sort this array by key and by date. Example would be October, 2016, November 2016, September, 2017, October 2014 Thank you

Roger
  • 597
  • 9
  • 32
  • You presumably want that in chronological order (i.e. Oct '16, Nov '16, Sep '17, Oct '17)? Where does this array come from? – ChrisGPT was on strike Nov 15 '16 at 22:17
  • Your question is not explicit about what you expect. Sorting keys is `ksort` why is this not ok ? – Simon Nov 15 '16 at 22:17
  • You probably want to look at `uksort()` to define a custom sorting function using the keys of your array. Also note that the date as key might not be a good idea if you have the same date twice. – Rizier123 Nov 15 '16 at 22:18
  • @Chris that is correct. It comes from a basic multidimensional array. I posted the whole code above – Roger Nov 15 '16 at 22:30
  • @Simon, hi it does not work because I need it in chronological order like chris states. Please refer to above code. Thanks – Roger Nov 15 '16 at 22:32
  • @Rizier123, please note that I uploaded the full code so you may understand why I need it that way. – Roger Nov 15 '16 at 22:42
  • @Roger Just use `kusort()` with a custom sorting function where you can convert your dates into timestamps and then you can simply compare the timestamps and determine the order. – Rizier123 Nov 15 '16 at 22:47
  • @Rizier123 thank you I solved it. – Roger Nov 15 '16 at 23:02

1 Answers1

1
<?php
$arr = array(
        "November, 2016" => "2",
        "October, 2016" => "5",
        "October, 2017" => "3",
        "September, 2017" => "6" 
);

//Sort by ascending date in the key
uksort($arr,function($a,$b){
  return strtotime(strtr($a,',',' '))<=>strtotime(strtr($b,',',' '));
});

//result
$expected = array (
  'October, 2016' => "5",
  'November, 2016' => "2",
  'September, 2017' => "6",
  'October, 2017' => "3",
);

var_dump($arr === $expected);  //bool(true)
jspit
  • 7,276
  • 1
  • 9
  • 17