Need assistance with a usort requirement - php

I've taken a look at a few googles and actually found this stack too: How to sort a date array in PHP
I'ts on the same wave length but i'm not sure i get it clearly. I've read over the phpnet documentation too... As I said, i'm feeling shakey from it.
I have an array:
Array
(
[0] => Array
(
[created] => 2012-06-06 21:26:25
)
[1] => Array
(
[created] => 2012-06-06 21:23:45
)
)
And I basically need to sort this array so that they are in date order.
How does this call back function work for usort? Any examples would be great!

function MySort($a, $b)
{
if ($a['created'] == $b['created']) return 0;
return $a['created'] < $b['created'] ? -1 : 1;
}
then use...
usort($myarray, "MySort");

usort just lets you sort using your own criteria. You can just simply do this:
usort($array, function($a, $b){
$a = strtotime($a['created']);
$b = strtotime($b['created']);
return $a-$b;
});

Related

how can i sort time from begin to end as time series [PHP]

I have mixed time of array to be sorting, it is a string "1:30","3:20","2:10" i want to be it "1:30","2:10","3:20"
is there anyone can help me for this question, i am confused. Sorting time array
array to be sorting: time
$arr = array("1:30","3:20","2:10");
after sorting: time
print like this: 1:30,2:10,3:20
You can use spaceship(<=>) operator for the same,
usort($arr, function ($a, $b) {
return strtotime($a) <=> strtotime($b);
});
Demo
O/P
Array
(
[0] => 1:30
[1] => 2:10
[2] => 3:20
)

PHP Sort string value range array in numeric Order

I have an array of range type values 257-1024, 1-256, 1025-2056. All these values are dynamically generated and positioned randomed. Before making an output I have to sort them in a numeric ASC order. Using sort or natsort function is giving the output as 1-256,1025-2056, 257-1024 as php recognise it as string. Is there a built in function with which this can be sorted/arranged in numeric range order i.e 1-256, 257-1024, 1025-2056
You can use natsort() function here.
$array = array("257-1024", "1-256", "1025-2056");
$a = natsort($array);
echo "<pre>";
print_r($array);
echo "</pre>";
Output:
Array
(
[1] => 1-256
[0] => 257-1024
[2] => 1025-2056
)
Hope this helps.
You can do like below using usort().
$array = ['257-1024', '1025-2056', '1-256'];
usort($array, function($a, $b){
return ((int)explode('-', $a)[1] < (int)explode('-', $b)[0]) ? -1 : 1;
});
print_r($array);
Output is below.
Array
(
[0] => 1-256
[1] => 257-1024
[2] => 1025-2056
)

Stable uasort - why is the order being reversed?

I'm using uasort to sort an array that looks like this:
Array
(
[2] => 0
[3] => 0
[4] => 0
)
I'm trying to sort by value, maintaining key association. In addition, I need to keep the original order if the values are the same.
So I'm doing the following:
uasort($arr, array($this, 'mysort'));
function mysort($a, $b){
if($a == $b){
return 0;
}
return ($a < $b) ? -1:1;
}
However, this gives the resulting array:
Array
(
[4] => 0
[3] => 0
[2] => 0
)
Why is the array being effectively reversed?
uasort is not a stable sort, that is, it doesn't maintain the order of equal values.
The manual's notes section contains a stable_uasort.

PHP: Sort array by value length

I am trying to sort an array by the length of characters in each value (and perhaps, if possible, in alphabetical order if two values have the same length of characters). For example:
Array ( [0] => this [1] => is [2] => a [3] => bunch [4] => of [5] => words;
I am trying to sort this array to look like:
Array ( [0] => a [1] => is [2] => of [3] => this [4] => bunch [5] => words;
How?
This should do it:
array_multisort(array_map('strlen', $array), $array);
Get the length of each string in an array by mapping strlen() using array_map()
Sort on the string lengths and sort the original array by the string length sorted array using array_multisort()
Looks like others have already answered but I started writing this so I'm going to post it, dang it! :)
You could take a look at usort
$data = ["this", "is", "a", "bunch", "of", "words"];
usort($data, function($a, $b) {
$difference = strlen($a) - strlen($b);
return $difference ?: strcmp($a, $b);
});
I'm using the Elvis operator ?: to just return the difference based on the string lengths if it's not 0. If it is 0, just return the return value of strcmp
You can use a custom sort function for this.
function mycmp($a, $b) {
$cmp = strlen($a) - strlen($b);
if ($cmp === 0)
$cmp = strcmp($a, $b);
return $cmp;
}
usort($array, 'mycmp');
See this:
$myarray = explode(" ", "this is a bunch of words");
usort($myarray, function($a, $b) { return strlen($a) - strlen($b) });
print_r($array);
Explanation: usort sorts in place (i.e. by reference) an array, given a custom comparator. A comparator must return a negative number if the first item should be before the second item, and a positive otherwise. A value of 0 means they are equal regarding the order.
You can add custom criteria, not related to strlen() when the lengths are the same. That's up to you and involves just an additional if block.
It is important to note that the function must define an order relationship, as in math:
criteria(a, b)>0 and criteria(b, c)>0 implies criteria(a, c)>0.
the same applies to the lower-than sign, lower-than-or-equal, greater-than-or-equal, and equal.
criteria(a, b) should have an opposite sign of criteria(b, a).
Another method:
$f = fn($s1, $s2) => strlen($s1) <=> strlen($s2);
usort($a, $f);
https://php.net/language.operators.comparison

Sorting multidimensional array values in PHP

I have the following array:
Array ( [0] => Array
( [name] => Jonah
[age] => 27 )
[1] => Array
( [name] => Bianca
[age] => 32 )
)
Is it possible to sort the sub-array values in [age] into some sort of order, such as lowest to highest or vice versa?
You can do this using usort:
usort($arr, function($a, $b)
{
return $a['age'] - $b['age']; // sorts lowest to highest
});
Swap $a and $b in the function to reverse the ordering.
I think this should be possible with bool usort ( array &$array , callback $cmp_function )
http://php.net/manual/en/function.usort.php
Just define a callback that sorts by the [age] key of the value.
This will work:
$ages = array();
foreach ($array as $value) {
$ages[] = $value;
}
array_multisort($values, SORT_ASC, $array);
Any way is good, but this is the "PHP way":
array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

Categories