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)
Related
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 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.
How can convert the result from eloquent to associative array. I need to select tow column and have one as key and another as value. Here is the closet I got, however the value is an array. I want it to be only "my_value" column.
$array = Post::select('my_key','my_value')->get()->keyBy('my_key')
You should use lists (Laravel 5.1) or pluck (Laravel 5.2+):
$array = Post::lists('my_value', 'my_key');
or
$array = Post::pluck('my_value', 'my_key');
I found a way to do so, I'm not sure whether it's proper way to do this performance wise though...
$array = Post::select('my_key','my_value')->get()->mapWithKeys(function ($item) {
return [$item['my_key'] => $item['my_value']];
})->toArray();
I am new to Laravel. I have a List that is being returned as an array in Blade
called personType, but the keys are all out of order.
I see the List being generated in the controller like this:
$personTypeList = PersonType::lists('per_type', 'id');
But this returns an array that is unsorted. I've tried ksort( $personTypeList) but it causes an error.
Anybody can shine a light on what I may be doing wrong?
how about adding an orderBy?
$personTypeList = PersonType::orderBy('per_type', 'desc')->lists('per_type', 'id');
You can use the method sort available when you are dealing with collections in laravel.
//will sort the collection by values, in ASC order.
$personTypeList = PersonType::lists('per_type', 'id') -> sort();
Hope this works for you.
You need to cast the collection to an Array.
$personTypeList = ['' => ''] + PersonType::lists('per_type', 'id')->toArray();
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