-1

I have the following multidimensional array that is being created from a loop. the 'date' is in the format of 'Ymd'. (I'm doing this because I have 2 different date fields coming in in different formats and I want to combine them and sort by a common format)

$myArray = array (
    'date' => $eDate,
    'title' => $title,
    'permalink' => $permalink
 );

How would I write a simple comparison function to pass into the usort() so that I could sort by that key of 'date'? So then I could do a simple foreach?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
acrane
  • 45
  • 1
  • 8

1 Answers1

1

@IMSoP is of course right, the answer has long since been given in a very comprehensive way. However, to help you on your way here is a short extract from the quoted answer containing the relevant bits for your problem:

usort($myArray,'cmp');

function cmp(array $a, array $b) { 
    $ad=$a['date']; $bd=$b['date'];
    if ($ad < $bd) {
        return -1;
    } else if ($ad > $bd) {
        return 1;
    } else {
        return 0;
    }
}
Community
  • 1
  • 1
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • Thank you so much! I realize now I have asked what has been asked tons of times. It was my first venture into usort and to see it applied to my snowflake, I know understand it. Thanks again. – acrane Sep 25 '15 at 17:32