I have a multi array that look like that:
Array (
[0] => Array(
[0] = Number;
),
[1] => Array(
[0] = Number;
)
)
And I want to sort it by the "Number" organ
For exmaple if I have:
Array (
[0] => Array(
[0] = 2;
),
[1] => Array(
[0] = 1;
)
)
I want to get:
Array (
[0] => Array(
[0] = 1;
),
[1] => Array(
[0] = 2;
)
)
I have more content in this arrays, I just wrote what need to be sort
How can I do that?
try:
Before PHP 5.3
function cmp($a, $b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
usort($array, "cmp");
Updated for PHP 5.3
usort($myArray, function($a, $b) {
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
});
Related
I can't sort my array so that numbers comes before - (hyphen).
My array today:
Array
(
[-] => Test
[0] => Test
[1] => Test
[2] => Test
)
The order I want:
Array
(
[0] => Test
[1] => Test
[2] => Test
[-] => Test
)
I have searched both here and on Google. But found no answers. I have tried experimenting with ksort() and usort(), but without success.
You could create your own compare function with uksort which handels such special cases.
uksort($a, function($a, $b) {
if (is_numeric($a) && is_numeric($b)) return $a - $b;
else if (is_numeric($a)) return -1;
else if (is_numeric($b)) return 1;
return strcmp($a, $b);
});
Use natural order sorting function
natsort()
example:
$arr = ['_', 6, 3, 5];
natsort($arr);
print_r($arr);
output:
Array
(
[2] => 3
[3] => 5
[1] => 6
[0] => _
)
If you want to sort by keys then you can use ksort() function with flag SORT_NATURAL ksort($arr, SORT_NATURAL);
example:
$arr = [
'_' => 'test',
6 => 'test',
3 => 'test',
5 => 'test'
];
ksort($arr, SORT_NATURAL);
function check($x, $y){
if(is_numeric($x) && !is_numeric($y))
return 1;
else if(!is_numeric($x) && is_numeric($y))
return -1;
else
return ($x < $y) ? -1 : 1;
}
$array = array("-", "1", "2", "3");
usort ( $array , 'check' );
I have an associative array in following this format:
Array
(
[214] => Array
(
[0] => 500 [1] => 350
)
[215] => Array
(
[0] => 500 [1] => 350
)
I need to sort the array in ascending order, this is what I need:
Array
(
[214] => Array
(
[0] => 350 [1] => 500
)
[215] => Array
(
[0] => 350 [1] => 500
)
This is what I have tried so far:
foreach($array_tarifa_dia as $row => $values)
{
foreach($values as $row1 => $values1)
{
$array[$row1] = $values;
}
}
array_multisort($array, SORT_ASC, $array_tarifa_dia);
function cmp($a, $b)
{
if ($a == $b)
{
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($array_tarifas1, 'cmp');
function cmp($a, $b)
{
if ($a[214] == $b[214])
{
return 0;
}
return ($a[214] < $b[214]) ? -1 : 1;
}
uasort($array_tarifas1, 'cmp');
Use sort() on each array within the array.
foreach ($arr as $key=>$val) {
if (is_array($val)) {
sort($arr[$key]);
}
}
See demo
I want to sort this array of arrays on the basis of final score, and preserve my keys. Can i use usort ? how ?
Array
(
[2253472] => Array
(
[noOfDays] => 1
[bestAns] => 1
[finalScore] => 13.5
)
[2253465] => Array
(
[noOfDays] => 1
[bestAns] => 0
[finalScore] => 50
)
[2253473] => Array
(
[noOfDays] => 1
[bestAns] => 0
[finalScore] => 23
)
[2253471] => Array
(
[noOfDays] => 1
[bestAns] => 0
[finalScore] => 89
)
[2253464] => Array
(
[noOfDays] => 1
[bestAns] => 1
[finalScore] => 0.5
)
[2253415] => Array
(
[noOfDays] => 6
[bestAns] => 0
[finalScore] => 0.333
)
)
All you need is
// Ascending
usort($data, function ($a, $b) {
return $a['finalScore'] - $b['finalScore'];
});
OR
// Descending
usort($data, function ($a, $b) {
return $b['finalScore'] - $a['finalScore'];
});
Yes you can if not preserving keys, you just need a user defined comparison method.
Use uasort() for key preservation : http://www.php.net/manual/en/function.uasort.php
Example:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
uasort($a, "cmp");
For your array, with some explanation
uasort($array, "cmp");
function cmp($a,$b){
if ($a['finalScore'] == $b['finalScore'])
{
// Same score, keep same
return 0;
}
elseif ($a['finalScore'] > $b['finalScore'])
{
// $a has higher score, move $b down array
return -1;
}
else {
// $a has lower score, move $b up array
return 1;
}
}
-1 moves it down, 0 leaves it, 1 pushes it up
I'm having a problem. I have a multidimensional array, that looks like this:
Array ( [0] =>
Array (
[0] => Testguy2's post.
[1] => testguy2
[2] => 2013-04-03
)
[1] => Array (
[0] => Testguy's post.
[1] => testguy
[2] => 2013-04-07
)
);
I want to sort the posts from the newest date to the oldest date, so it looks like this:
Array ( [1] => Array (
[0] => Testguy's post.
[1] => testguy
[2] => 2013-04-07
)
[0] => Array (
[0] => Testguy2's post.
[1] => testguy2
[2] => 2013-04-03
)
);
How do I sort it?
function cmp($a, $b){
$a = strtotime($a[2]);
$b = strtotime($b[2]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");
Or for >= PHP 7
usort($array, function($a, $b){
return strtotime($a[2]) <=> strtotime($b[2]);
});
You can do it using usort with a Closure :
usort($array, function($a, $b) {
$a = strtotime($a[2]);
$b = strtotime($b[2]);
return (($a == $b) ? (0) : (($a > $b) ? (1) : (-1)));
});
I'm just stepping away from my desk for the day so I can't offer specifics. But here's a good place to get started that includes examples: array_multisort
$dates = array();
foreach($a AS $val){
$dates[] = strtotime($val[2]);
}
array_multisort($dates, SORT_ASC, $a);
Can anyone tell me how to sort an array by key?
I want to sort this array by price.
this is my input array format
Array
(
[0] => Array
(
[house_data] => Array
(
[id] => 532
[max_person] => 8
[max_bedrooms] => 4
)
[image] => uploads/123.jpg
[price] => 1950
)
[1] => Array
(
[house_data] => Array
(
[id] => 531
[max_person] => 8
[max_bedrooms] => 5
)
[image] => uploads/1234.jpg
[price] => 1495
)
}
Try usort (http://php.net/manual/en/function.usort.php)
You should have something like:
function cmp($a, $b)
{
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
usort($table, "cmp");
For making it one dimensional use serialize($array) function , it goes like this :$a = array() ; //your multidimensional array$b = array(); //output arrayforeach ($a as $key=>$value){ $b[] = serialize($value);}echo $b ;
Use the array_multisort() function for multidimensional array sorting.