How to get only 5 newest articles from array?
For example:
usort($articles, function ($item1, $item2) {
return $item2['created_at'] <=> $item1['created_at'];
});
This will sort them descending and I don't need to show all of them.
What would be the best way to do this? Is it possible without looping again and storing in new array?
Using array_slice on the sorted array should do.
usort($articles, function ($item1, $item2) {
return $item2['created_at'] <=> $item1['created_at'];
});
$fiveFirst = array_slice($articles, 0, 5);
Related
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)
I want to sort php object array in which i have to consider two different keys while sorting, if value of two users is same then consider another key for those values.
given array example
[{'id':'1','total_number':'90','previous_number':'75'},
{'id':'2','total_number':'80','previous_number':'91'},
{'id':'3','total_number':'80','previous_number':'84'},
{'id':'4','total_number':'80','previous_number':'96'},
{'id':'5','total_number':'95','previous_number':'80'}
]
Result array:
[{'id':'5','total_number':'95','previous_number':'80'},
{'id':'1','total_number':'90','previous_number':'75'},
{'id':'4','total_number':'80','previous_number':'96'},
{'id':'2','total_number':'80','previous_number':'91'},
{'id':'3','total_number':'80','previous_number':'84'}
]
You can use a custom sort function to achieve the desired result with the help of usort function in PHP https://www.php.net/manual/en/function.usort.php
// custom sort function
function mySort($a, $b) {
// is total_number same?
if ($a->total_number === $b->total_number) {
// use previous_number to sort
if ($a->previous_number === $b->previous_number) {
return 0;
}
// sort desc
return $a->previous_number > $b->previous_number ? -1 : 1;
}
// sort desc
return $a->total_number > $b->total_number ? -1 : 1;
}
// call the function
usort($array, 'mySort');
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);
}
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
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