I have an array that holds an array of news articles. I have them filtered down to date, but now I’d like them sorted by time day they were published.
$articles = [
0 => ( array('pubTime' => 1342537795, 'title' => 'n/a', 'author' => 'n/a', 'content' => 'n/a')),
1 => ( array('pubTime' => 1442537744, 'title' => 'n/a', 'author' => 'n/a', 'content' => 'n/a')),
2 => ( array('pubTime' => 1342537795, 'title' => 'n/a', 'author' => 'n/a', 'content' => 'n/a'))
];
I’ve tried several different things, none of which have any effect. I should mention I’m within a class scope. Here’s the code from a couple of my attempts:
usort($articles, function($a, $b) {
return $b['postPubDate'] - $a['postPubDate']);
});
I thought that class scope may’ve been a problem so I also tried it like this, without success:
usort($articles, array($this, 'customSort'));
public function customSort($a, $b)
{
return $b['postPubDate'] - $a['postPubDate']);
}
usort needs to return a boolean, right now you are returning an integer. From all of the times I have used it, I usually utilize < > or <= >= in my return statement. So depending on whether you want it returned as oldest to newest or newest to oldest, you would use < or >
Also, you are trying to read postPubDate, but I don’t see that in your array, I see pubTime.
Here is the function I used (you may need to change < to > to get the order you want
usort($articles, function($a, $b)
{
return $b['pubTime'] < $a['pubTime'];
}
);
Thanks for the reply and for catching that typo. Correct, “postPubDate” was meant to read “pubTime.”
I’m not sure what I’m doing wrong though. I’ve changed the operand as suggested but still coming back with the same results. Here’s my stripped down code along with my test data. It’s still being returned in the order it is defined.
$feed['10-17'][] = array(
'pubTime' => 1342534067,
'title' => 'mytitle 2',
'description' => 'show me second'
);
$feed['10-17'][] = array(
'pubTime' => 1442522773,
'title' => 'mytitle 3',
'description' => 'show me last'
);
$feed['10-17'][] = array(
'pubTime' => 1142522773,
'title' => 'mytitle 1',
'description' => 'show me first'
);
foreach($feed as $articles => $a)
{
usort($a, function($a, $b) {$b['pubTime'] > $a['pubTime']; });
var_dump($feed);
}
brandonBuster:
Thanks for the reply and for catching that typo. Correct, “postPubDate” was meant to read “pubTime.”
I’m not sure what I’m doing wrong though. I’ve changed the operand as suggested but still coming back with the same results. Here’s my stripped down code along with my test data. It’s still being returned in the order it is defined.
$feed['10-17'][] = array(
'pubTime' => 1342534067,
'title' => 'mytitle 2',
'description' => 'show me second'
);
$feed['10-17'][] = array(
'pubTime' => 1442522773,
'title' => 'mytitle 3',
'description' => 'show me last'
);
$feed['10-17'][] = array(
'pubTime' => 1142522773,
'title' => 'mytitle 1',
'description' => 'show me first'
);
foreach($feed as $articles => $a)
{
usort($a, function($a, $b) {$b['pubTime'] > $a['pubTime']; });
var_dump($feed);
}
You want to do a var_dump on $a. However, I highly suggest renaming your variables to the following
$feed['10-17'][] = array(
'pubTime' => 1342534067,
'title' => 'mytitle 2',
'description' => 'show me second'
);
$feed['10-17'][] = array(
'pubTime' => 1442522773,
'title' => 'mytitle 3',
'description' => 'show me last'
);
$feed['10-17'][] = array(
'pubTime' => 1142522773,
'title' => 'mytitle 1',
'description' => 'show me first'
);
foreach($feed as $key => $articles)
{
usort($articles, function($a, $b) { return $b['pubTime'] > $a['pubTime']; });
var_dump($articles);
}
Thanks again for your reply. But I’m still getting the articles returned in their defined order.
You forgot the return statement in your anonymous function (I even missed it and spent 15 minutes trying to figure out why it wasn’t working.
Also, if you want to maintain the same array and not want to store the sorted information in a separate array, you can use this:
$feed['10-17'][] = array('pubTime' => 1342534067, 'title' => 'mytitle 2', 'description' => 'show me second');
$feed['10-17'][] = array('pubTime' => 1442522773, 'title' => 'mytitle 3', 'description' => 'show me last');
$feed['10-17'][] = array('pubTime' => 1142522773, 'title' => 'mytitle 1', 'description' => 'show me first');
foreach($feed as $key => $feedArticles)
{
usort($feed[$key], function($a, $b) { return $b['pubTime'] < $a['pubTime']; });
}
var_dump($feed);
Beautiful. Your tip for saving the sorted array rather than just making a copy of it was right on point too. Exactly what I was trying to accomplish. Big thanks for all your help!
Not a problem and sorry I didn’t catch the missing return statement sooner (if I had, my earlier reply would have worked for you before I updated it).