How to reorder an array by named index - php

In the function below, the $myrecentposts variable holds a collection of the 5 latest posts. How can I take these 5 posts and reorder them before they are written to the for-loop so that they are ordered by their post-title index in the array?
function getLatestPostsAndSort($post)
{
$myrecentposts = get_posts(
array('post__not_in' => get_option('sticky_posts'),
'numberposts' => 5
)
//NEED TO RESORT THE ARRAY HERE BY [POST-TITLE]
foreach($myrecentposts as $idxrecent=>$post)
{
?><li><?php the_title(); ?></li><?php
}
}
PS: the obvious suggestion might be to pull them from the database in the desired order. However, the get_posts() method offers no such option. The default order is by post date (latest first). If I change the order to "title", it does not take post date into consideration and rather just pulls the entire post collection and orders them by title.

Generally speaking, you can sort an array like:
usort($myrecentposts, function($a, $b) {
return strcmp($a['title'], $b['title']);
});
You need to adjust title to be whatever the actual name of the key is.
If this is wordpress, then based on a quick Google search, it looks like it might be:
usort($myrecentposts, function($a, $b) {
return strcmp($a->post_title, $b->post_title);
});
If you don't have PHP 5.3, then you'll need to move that anonymous function into a real one like:
function sort_post_by_title($a, $b) {
return strcmp($a->post_title, $b->post_title);
}
usort($myrecentposts, 'sort_post_by_title');

http://php.net/manual/en/function.usort.php
You need to use usort();

You can use php's usort function: http://php.net/manual/en/function.usort.php

Related

Sorting laravel collection for times

Sorting a unique collection by value. I've tried sortBy but instead of the key need to sort by the value
return ($c->flatten()->unique());
Output: Need to go in chronological order
{"0":"9am","1":"10am","2":"11am","3":"1pm","4":"2pm","5":"5pm","10":"3pm","14":"12pm"}
Assuming that $c is the collection you meant, this should work:
$sorted = $c->sortBy(function($value, $key) {
return strtotime($value);
});
Sources:
Laravel 5 Collections Sorting
How to sort time by meridiem (AM/PM)

Detect if values are repeated and apply order criteria using Laravel and collections

How can I detect if a value is repeated in a collection? And if it repeats, apply a criterion of order for the following value?
I have the following collection where if the "total" value is repeated I have to sort by the value "next" and if the value "next" also repeats, I must sort by the value "subsequent"
Following the criteria of order, it would be as follows:
For now in my code I only have a normal collection:
$sales = Sale::select('total', 'next', 'subsequent', 'person_id')->with('person')->get();
Thanks
You may try this (using Collection::sort() which uses PHP's usort under the hood):
$sales = 'Get result...';
$sorted = $sales->sort(function ($a, $b) {
return strcmp($a->total, $b->total)
?: strcmp($a->next, $b->next)
?: strcmp($a->subsequent, $b->subsequent);
});

How can I sort this Collection using an array order?

How can I sort the $item type based on the order? This is what I currently have:
$order = ['AA', 'zl', 'Dr', 'co', 'og'];
$items->sort(
function ($a, $b) use ($order) {
return strcmp($b->type, $a->type) // how do I apply the order here?
?: strcmp($b->date, $a->date);
}
);
Basically it's a two column sort where it first sorts by type and then sorts by date. The type would be sorted using the array order rather than the typical alphabetical order.
You could compare the keys in $order corresponding to the type of each item.
return ($order[$b->type] - $order[$a->type])
?: strcmp($b->date, $a->date);
This should work if all of your items have types contained in $order. If not, you'll get undefined index errors which will mess up the sort. But since you haven't specified how items like that should be sorted in your question, I'm assuming that they all do.

sort array of objects by two fields in php

i have an array of objects like this : http://pastebin.ca/3217309
i want to sort this array to objects with come first then others sort by date
currently I'm using this to sort array
function cmp($a, $b)
{
return strcmp($a->date, $b->date);
}
usort($data, "cmp");
and works good but it only sort array by date and i want to in ordered array objects with pin=1 come first then other objects come after pins.
i hope my question is clear,sorry for bad english !
If you want to sort after an other criteria, you should just add the condition to your sort function:
if ($a->pin != $b->pin) return $a->pin == 1 ? -1 : 1;
this condition only applies for $items with unequal pin properties. Combine your old compare function with this one would result in the following
function cmp($a, $b) {
if ($a->pin != $b->pin) return $a->pin == 1 ? -1 : 1;
return strcmp($a->date, $b->date);
}

PHP sort array through personalized order

I need to sort an array, I've done this before but it has been easy because the array had numbers or letters to sort in ascedning/descending or alphabetical order.. In this case i have an array of which each element has 3 values, eg:
array[0]=code=1234
=description='example array'
=orderCode=P
array[1]=code=1235
=description='example array1'
=orderCode=A
.
.
.
Now i need to order theese reading the orderCode value in this order: P,I,B,C,A,S,D.
The way i thought of getting arround it was to add another value to the array and to something like:
if($array[$c]['orderCode'] == 'P')
$array[$c]['newOrderCode'] = 0;
if($array[$c]['orderCode'] == 'I')
$array[$c]['newOrderCode'] = 1;
if($array[$c]['orderCode'] == 'B')
$array[$c]['newOrderCode'] = 2;
or a switch case and then order it by the new value. This would work, but my question is, is there a function I can pass the array to and an orderring string or something?
Thank you,
James
In php 5.3 and above you can use usort with a closure.
$order = array('P','I','B','C','A','S','D');
usort($array, function ($a, $b) use ($order){
return array_search($a["orderCode"], $order) - array_search($b["orderCode"], $order);
});
prior to that you have to create a sorter function
function orderCode_sorter($a, $b){
$order = array('P','I','B','C','A','S','D');
return array_search($a["orderCode"], $order) - array_search($b["orderCode"], $order);
}
usort($array, "orderCode_sorter");
use function user defined and choise sort by key or value you need see all list function here: http://www.php.net/manual/en/array.sorting.php

Categories