Trying to add a sort function into an existing snippet, though keeps saying “Fatal error: Can’t use function return value in write context in… line 88”:
function sortFunction( $a, $b ) {
return strtotime($a["last_modified"]) - strtotime($b["last_modified"]);
}
$children = $topPage->getChildren(null,true);
if (!empty($children)) {
//line 88
foreach($children as usort($child, "sortFunction")) { ...
You can’t use a function as part of the foreach statement - what you have to do instead is get the value and then sort it, like this:
foreach ($children as &$child) {
usort($child, 'sortFunction');
}
Note that I’ve assumed you want to sort the child arrays in-place, so putting an ampersand infront of the variable name causes PHP to reference the original child array rather than making a copy.
Trying to add a sort function into an existing snippet, though keeps
saying “Fatal error: Can’t use function return value in write context
in… line 88”:
usort, like most PHP array manipulation functions, receives the array to work upon by reference, and returns boolean to indicate if it successfully performed it’s operation. That isn’t the cause of the error here, but that’s your first misconception on how the function works.
You can’t use a function as part of the foreach statement
That isn’t true. You can, what you can’t do is write to it with foreach, which is what the OP’s code is trying to do and what is kicking the error. If the function returns an array it’s perfectly legal to iterate off the return, for example,
foreach (glob('./*.php') as $file) {}
This is only safe if you are absolutely certain the function will either return an array, or throw an exception. Returning an empty array will result in the iteration not being performed. Glob is an off the top of my head example and isn’t an ideal example because it returns “false” if the file search pattern doesn’t match anything.
The part of the foreach to the right of “as” is being written to, and you can’t write to functions. It’s just as illegal as this
usort($child, 'sortFunction') = $vars;
If I understand your intent correctly you’re using the wrong function. usort won’t help - use array_multisort. You also need to normalize the return of that function to be arrays only instead of a mix of arrays and objects. Finally, if this is coming out of a database it will be FAR faster to sort at query time than in PHP.
Thanks guys.
Are you saying that fretburners example won’t work?
And instead I should use:
foreach ($children as &$child) {
if (is_array($child)) {
array_multisort($child, 'sortFunction');
}
}
Some good information here thanks, though starting to get a bit heavy for a basic sort
As things stand I don’t have access to the database logic as everything is included from the CMS, else I would do the sorting at database level, as you sty, much better.
/*
* First get the pages.
*/
$pages = $topPage->getChildren(null, true);
/*
* Extract the last Modified Column
*/
$lastModified = array();
foreach ($pages as $key => $row) {
$modified[$key] = $row->last_modified;
}
/*
* Now sort the pages array on that column. That each row is an object won't matter.
*/
array_multisort($modified, SORT_STRING, SORT_DESC, $pages);
You don’t need strtotime because the lastModified value is already in the correct format to be string sorted. If you need ascending order use SORT_ASC. Since array_multisort receives by referrence $pages will be in the desired order immediately after this code executes.
After you sort the pages you’re going to need to start a foreach loop to iterate over them for each article template you echo out. The loop will be
foreach ($pages as $child)
Beyond that, you need to work this out on your own because I fear further help will do more harm than good by not letting you learn this for yourself. The sortFunction you made isn’t needed anymore and can be deleted.
Ha correct, causing some headaches already
I had everything working previous, I needed to change $child->column to $row->column… then everything worked. I’ve now changed something and back to square one.
Though as you say, good learning curve a suppose.
I’m also needing a limit on the articles returned, though might try some javascript for that, though is it hard to add a limit?
This is showing the object for the single article, the current page. Just one item.
What I’m trying to do is show a list of related articles below this page/current article, if that makes sense. So with what we can see here, it seems right, I think. What do you make of it?