I have a two-dimensional array given from a db data in a custom order.
To sort the main array I'm using this:
usort($arr, function ($a, $b) {
return $a[0] <=> $b[0];
});
json_encode result:
[["2016","0000-00-00"],["2017","0000-00-00"],["2018","0000-00-00"]]
Now I need to sort items by descending order having this result:
[["2018","0000-00-00"],["2017","0000-00-00"],["2016","0000-00-00"]]
I tried to change return $a[0] <=> $b[0]; into this:
return $a[0] > $b[0];
return $a[0] < $b[0];
return $a[0] - $b[0];
return $b[0] <=> $a[0];
Nothing works. Any help?
This works (used $b[0] <=> $a[0] for sorting):
$array = json_decode('[["2016","0000-00-00"],["2017","0000-00-00"],["2018","0000-00-00"]]', TRUE);
usort($array, function($a, $b) { return $b[0] <=> $a[0]; });
print_r($array);
The result is sorted descending:
[0] => Array
(
[0] => 2018
[1] => 0000-00-00
)
[1] => Array
(
[0] => 2017
[1] => 0000-00-00
)
[2] => Array
(
[0] => 2016
[1] => 0000-00-00
)
$a[0] < $b[0] works for me.
usort($array, function($a, $b) {
return $a[0] < $b[0];
});
Live demo here
Related
My goal is to sort an array first by the string length and after that sort it again by character value without changing the priority of the length.
Here is my code:
$arr=array("an","am","alien","i");
usort($arr, function ($a, $b) { return (strlen($a) <=> strlen($b)); });
print_r($arr);
I am getting :
Array ( [0] => i [1] => an [2] => am [3] => alien )
...almost but not there)
You're missing the logic to account for sorting by character value. You can add that logic into your custom sort method:
// if lengths are the same, then sort by value
if (strlen($a) == strlen($b)) {
return $a <=> $b;
}
// otherwise, sort by length
return (strlen($a) <=> strlen($b));
You can combine this into a single line by using a ternary:
return strlen($a) == strlen($b) ? $a <=> $b : strlen($a) <=> strlen($b);
Full example (https://3v4l.org/msISD):
<?php
$arr=array("aa", "az", "an","am", "ba", "by", "alien","i");
usort($arr, function ($a, $b) {
return strlen($a) == strlen($b) ? $a <=> $b : strlen($a) <=> strlen($b);
});
print_r($arr);
outputs:
Array
(
[0] => i
[1] => aa
[2] => am
[3] => an
[4] => az
[5] => ba
[6] => by
[7] => alien
)
Array (
[0] => Array (
[2] => 9.6
)
[1] => Array (
[497] => 11.666666666667
)
[2] => Array (
[451] => 34
)
[3] => Array (
[459] => 8.8
)
[4] => Array (
[461] => 22.5
)
)
I have this array.
How can I sort it by number value?
I tried
usort($array, function ($a, $b)
{
return $a[0] < $b[0];
});
But doesn't work.
First things first... from the manual
value_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
Emphasis on the "integer" part.
From PHP 7, you can use the spaceship operator for this.
You need to do some extra work though to address your non-sequential array keys. You can get the first value using reset()
usort($array, function($a, $b) {
return reset($b) <=> reset($a);
});
Demo ~ https://3v4l.org/uni3v (doesn't work in PHP 5.x)
The pre PHP 7 version can be achieved with a simple subtraction. For example
return reset($b) - reset($a);
Try this:
$numbers = [1, 3, 2];
usort($numbers, function ($a, $b) {
return $a < $b ? 1 : ($a === $b ? 0 : -1);
});
print_r($numbers);
Result is:
Array
(
[0] => 3
[1] => 2
[2] => 1
)
Array
(
[0] => Jane Smith
)
Array
(
[0] => John Paul
)
Array
(
[0] => Jennifer
)
Array
(
[0] => Paolo
)
Array
(
[0] => Delilah
)
foreach($name as $a){
print_r($a);
}
Is it possible to alphabetically arrange this array?
How can i use the sort() in here?
Try this :
$array = your array
$result = call_user_func_array('array_merge', $array);
sort($result);
echo "<pre>";
print_r($result);
Try this:
<?php
$ar1 = array("Jane Smith", "John Paul ", "Jennifer", "Paolo","Delilah");
function alphasort($a, $b) {
if ($a['name'] == $b['name']) {
return 0;
}
return ($a['name'] < $b['name']) ? -1 : 1;
}
usort ($ar1,'alphasort');
echo '<pre>';
print_r($ar1);
?>
Result:
Array
(
[0] => Delilah
[1] => Jane Smith
[2] => Jennifer
[3] => John Paul
[4] => Paolo
)
Try like
$array = your array;
asort($array);
Try this LINK
Since it looks like you're trying to sort an array of arrays of strings instead of an array of strings, you cannot use sort().
$array = array(array('Jane Smith'), array('John Paul'), array('Jennifer'));
function cmp($a, $b)
{
$a = $a[0];
$b = $b[0];
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");
foreach($name as $a){
print_r($a);
}
Example code based on this documentation.
Take a look at here for all kinds of PHP array sorting. But for your specific question after doing array_merge() on all you arrays to have a single onw, either sort() or asort() should work just like this:
$all=array();
foreach($name as $a){
$all=array_merge($all, $a);
}
sort($all);
print_r($all);
OR
$all=array();
foreach($name as $a){
$all=array_merge($all, $a);
}
asort($a);
print_r($a);
I have array:
$array = array(array('2012-12-12', 'vvv'), array('2012-12-14', 'df'),array('2012-12-10', 'vvv'),array('2012-12-11', 'vvv'));
Array
(
[0] => Array
(
[0] => 2012-12-12
[1] => vvv
)
[1] => Array
(
[0] => 2012-12-14
[1] => df
)
[2] => Array
(
[0] => 2012-12-10
[1] => vvv
)
[3] => Array
(
[0] => 2012-12-11
[1] => vvv
)
)
http://codepad.org/gxw2yKMU
is possible to sort this with dates DESC? For this example should be:
$array[1] //2012-12-14
$array[0] //2012-12-12
$array[3] //2012-12-11
$array[2] //2012-12-10
For me the best way is use embedded functions for PHP, but how? :)
You can use usort with a custom function. If you're on PHP < 5.3 you'll need a named function rather than, as I have, an anonymous one.
$array = array(
array('2012-12-12', 'vvv'),
array('2013-12-14', 'df'),
array('2012-12-14', 'df'),
array('2012-12-10', 'vvv'),
array('2012-12-11', 'vvv')
);
usort($array, function($a, $b) {
if ($a[0] == $b[0]) return 0;
return ($a > $b) ? -1 : 1;
});
print_r($array);
You should be able to use usort
usort( $array, 'sortFunction' );
function sortFunction( $a, $b ) {
if( $a[0] == $b[0] )
return 0;
return ( $a[0] > $b[0] ? return -1 : 1 );
}
You can use array_multisort() :
foreach ($array as $key => $row) {
$dates[$key] = $row[0];
}
array_multisort($dates, SORT_DESC, $array);
First, you put out all dates in a new array. Then, array_multisort() will sort the second array ($array) in the same order than the first ($dates)
I have a data structure that looks like
Array
(
[0] => Array
(
[0] => something
[1] => 1296986500
)
[1] => Array
(
[0] => something else
[1] => 1296600100
)
[2] => Array
(
[0] => another thing
[1] => 1296831265
)
)
I'm trying to sort the array based off of the integer which is a unix timestamp. The following function looks right to me but is not sorting the way I want.
function cmp($a, $b)
{
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
NOTE
when calling this function within a class the OO syntax is the following
uasort($_data, array($this, 'cmp'));
That sorts your timestamps in ascending order; for descending order, flip the second comparison (i.e. change $a[1] < $b[1] to $a[1] > $b[1]):
function cmp($a, $b)
{
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] > $b[1]) ? -1 : 1;
}
You can setup time stamp as pivot. And use array_multisort().
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$time[$key] = $row[1]; //unix timestamp
}
array_multisort( $time, SORT_ASC, $data);
?>