This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 years ago.
I want to sort the array on date basis.
I used the below code to sort the array
<?php
$a=array("14-10-2013","15-10-2013","16-10-2013","13-11-2013","17-11-2013","18-10-2013","19-10-2013");
array_multisort($a);
print_r($a);
?>
this gives me the result
Array ( [0] => 13-11-2013 [1] => 14-10-2013 [2] => 15-10-2013 [3] => 16-10-2013 [4] => 17-11-2013 [5] => 18-10-2013 [6] => 19-10-2013 )
this is not the correct order.
the correct order should be
Array ( [0] => 14-10-2013 [1] => 15-10-2013 [2] => 16-10-2013 [3] => 18-10-2013 [4] => 19-10-2013 [5] => 13-11-2013 [6] => 17-11-2013 )
What should i have to do for the correct sequence of the date?
use this code :
$a=array("14-10-2013","15-10-2013","16-10-2013","13-11-2013","17-11-2013","18-10-2013","19-10-2013");
usort($a, "sortFunction");
print_r($a);
function sortFunction( $a, $b ) {
return strtotime($a) - strtotime($b);
}
OUTPUT
Array
(
[0] => 14-10-2013
[1] => 15-10-2013
[2] => 16-10-2013
[3] => 18-10-2013
[4] => 19-10-2013
[5] => 13-11-2013
[6] => 17-11-2013
)
You have a few options here.
Reverse the date: 2013-11-13 for example. You are sorting a string. So it starts with the lowest number. That will only work when you have Year-Month-Date.
You can also use timestamps. So from a timestamp, sort all the value's. After that use:
foreach($a as $key => $value) {
$a[$key] = date("d-m-Y", $value);
}
print_r($a);
Related
I have a big array as $orderArr:
$orderArr = array("IZQ", "AOH", "VNP", "ICW", "IOQ", "BXP", "SHH", "EAY", "ZAF", "CUW");
which looks like
Array ( [0] => IZQ [1] => AOH [2] => VNP [3] => ICW [4] => IOQ [5] => BXP [6] => SHH [7] => EAY [8] => ZAF [9] => CUW )
and I have two small arrays as $subArr1 and $subArr2:
$subArr1 = array("VNP", "BXP", "ICW", "IZQ");
$subArr2 = array("ZAF", "IZQ", "AOH");
looks like
Array ( [0] => VNP [1] => BXP [2] => ICW [3] => IZQ )
Array ( [0] => ZAF [1] => IZQ [2] => AOH )
Both small arrays (sub array) own elements belong to the big array.
I want to sort two small arrays according to the order of big array, as following:
Array ( [0] => IZQ [1] => VNP [2] => ICW [3] => BXP )
Array ( [0] => IZQ [1] => AOH [2] => ZAF )
I am looking for the simplest codes to do it in php. Any suggestions are welcome.
Probably the simplest would be to compute the intersection and it will return in the order of the first array:
$subArr1 = array_intersect($orderArr, $subArr1);
That will return with the keys of the first array; if you want to reindex instead:
$subArr1 = array_values(array_intersect($orderArr, $subArr1));
You could use usort to sort based on array position:
usort($subArr1, function ($a, $b) use ($orderArr) {
return (array_search($a, $orderArr) < array_search($b, $orderArr)) ? -1 : 1;
});
var_dump($subArr1);
This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
Closed 5 years ago.
I have the array below
Array
(
[Prod1] => Array
(
[0] => $1,167,788.03
[1] => 26,872
[2] => 73.42
[3] => 19.0%
[4] => $1,134,106.83
[5] => $1,681,843.02
[6] => $3,098.65
[7] => $42.20
[8] => $-19.55
[9] => $-9.60
[10] => $43.46
[11] => 0.97
)
[Prod2] => Array
(
[0] => $6,730.84
[1] => 161
[2] => 0.44
[3] => 13.7%
[4] => $4,783.41
[5] => $6,755.61
[6] => $13.07
[7] => $29.71
[8] => $-27.30
[9] => $-21.50
[10] => $41.81
[11] => 0.71
)
[Prod3] => Array
(
[0] => $2,498,984.47
[1] => 30,409
[2] => 83.08
[3] => 21.5%
[4] => $3,026,866.16
[5] => $3,850,645.25
[6] => $8,270.13
[7] => $99.54
[8] => $-21.33
[9] => $-8.19
[10] => $82.18
[11] => 1.21
)
}
I am trying to sort it on descending order based of the index[0] and tried using different PHP built in functions but I was not successful on that.
Basically the desired result would be in the following order Prod3, Prod1, Prod2.
What would be the best way for solution for this?
Thanks
supposing $arr is the array you've displayed above, the below code should work in your case
$tmp_arr = array();
$sorted_arr = array();
foreach ($arr as $key => $val_array) {
//removing $ and ,
$first_index_without_dollar = str_replace(array("$", ","), array("", ""), $val_array[0]);
//getting string as number in order to sort
$first_index_without_dollar = number_format((float) $first_index_without_dollar, 2, '.', '');
$tmp_arr[$first_index_without_dollar] = $key;
}
//sorting by key descending
krsort($tmp_arr);
foreach ($tmp_arr as $val) {
$sorted_arr[$val] = $arr[$val];
}
I don't know if usort is the best way but the user function is fairly simple:
usort($array, function ($a, $b) {
$x = (int)str_replace(['$',',','.'],'',$a[0];
$y = (int)str_replace(['$',',','.'],'',$b[0];
if ($x === $y) return 0;
return ($x < $y) ? 1 : -1;
});
Edit: I missed the format on the values the first time. And I would not have found it without Mohamad Attat's answer. The str_replace calls should fix it, iff you always have two decimals in your dollar amounts.
I know this question has been asked a hundred times, and I've gone through multiple answers and am not getting the correct results.
I am trying to order the following row by date, oldest first:
Array(
[0] => Array
(
[0] => '3173'
[1] => 'Contact - 12-6-14 Outstanding invoice'
[2] => '16/06/2014'
[3] => '204'
)
[1] => Array
(
[0] => '3167'
[1] => 'Contact - Outstanding invoice'
[2] => '13/06/2014'
[3] => '207'
)
[2] => Array
(
[0] => '3497'
[1] => 'New Site - Keri Keri'
[2] => '25/11/2014'
[3] => '43'
)
[3] => Array
(
[0] => '2023'
[1] => 'Analysis'
[2] => '17/06/2014'
[3] => '355'
)
[4] => Array
(
[0] => '2641'
[1] => 'PSS'
[2] => '20/02/2014'
[3] => '321'
)
)
I have tried things such as the below with no luck.
function cmp($a, $b){
return $b[2] - $a[2];
}
usort($urgent_array, "cmp");
Any help on this one would be really appreciated :)
First, you want to compare, not subtract:
function cmp($a, $b) {
if ($a[2] == $b[2])
return 0;
return ($a[2] > $b[2]) ? 1 : -1;
}
usort($urgent_array, "cmp");
Then, date ordering works best if you use the Y-m-d format:
2014-06-13 etc.
Change your compare function to
function cmp($a, $b){
$a_date = strtotime(str_replace('/', '-', $a[2]));
$b_date = strtotime(str_replace('/', '-', $b[2]));
return $a_date - $b_date;
}
You want to use strtotime because subtracting strings doesn't really mean anything. The str_replace is because PHP expects dashes for dd-mm-yyyy format.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 years ago.
I have this array of arrays,
Array
(
[0] => Array
(
[id] => 1
[title] => AP-2 (1)
)
[1] => Array
(
[id] => 2
[title] => AC-1 (2)
)
[2] => Array
(
[id] => 3
[title] => AB-3 (1)
)
[3] => Array
(
[id] => 4
[title] => AD-2 (3)
)
[4] => Array
(
[id] => 5
[title] => AE-2 (1)
)
),
and I need to sort it in a way in which it will look like this,
Array
(
[0] => Array
(
[id] => 1
[title] => AB-3 (1)
)
[1] => Array
(
[id] => 2
[title] => AC-1 (2)
)
[2] => Array
(
[id] => 3
[title] => AD-2 (3)
)
[3] => Array
(
[id] => 4
[title] => AE-2 (1)
)
[4] => Array
(
[id] => 5
[title] => AP-2 (1)
)
)
What happened here is basically, sort the arrays using the title key alphabetically or maybe sort it using natsort() or natcasesort(). How would I actually do the sorting? Thanks in advance.
function sorter($key){
return function ($a, $b) use ($key) {
return strcmp($a[$key], $b[$key]);
};
}
usort($arr, sorter('title'));
var_dump($arr);
For versions of PHP prior to 5.3, use:
function sorter($arr, $index) {
foreach($arr as $key => $value) {
$arr2[$key] = strtolower($value[$index]);
}
asort($arr2);
foreach($arr2 as $key =>$value) {
$arr3[] = $arr[$key];
}
return $arr3;
}
var_dump(sorter($arr, 'title'));
So I have an array such as this one:
Array
(
[-1] => Array
(
[3] => 3
[1] => 1
[6] => 6
[7] => 7
[5] => 5
)
)
It also contains some other keys that should not be modified.
I'd like to the numbers which are in a second array to come first (in the order of that second array), and then will be the numbers that don't exist in the second array, if any.
So for that matter, the second array would be:
Array
(
[0] => 6
[1] => 5
[2] => 3
)
And the final array should be as follows (please remember, there are some more keys inside of that array that should stay as they are):
Array
(
[-1] => Array
(
[6] => 6
[5] => 5
[3] => 3
[1] => 1
[7] => 7
)
)
Any ideas how that can be done?
Thanks!
It's not and shouldn't be termed as sorting but may be this code snippet may help you do what you want to:
$a1 = Array ( [-1] => Array ( [3] => 3 [1] => 1 [6] => 6 [7] => 7 [5] => 5 ) );
$a2 = Array ( [0] => 6 [1] => 5 [2] => 3 );
$sorted = getSortedArray($a1[-1] , $array2);
function getSortedArray($array1 , $array2){
$temp = Array();
$count = 0;
$totalKeys = sizeof($array2);
for($i=0;$i<sizeof($array2);$i++){
$temp[i] = $array1[$array2[i]];
unset($array1[$array2[i]]);
}
while($count!=sizeof($array1))
$temp[$totalKeys++] = $array1[$count++];
return $temp;
}
I believe the function you're looking for is called array_multisort().
array_multisort() can be used to sort
several arrays at once, or a
multi-dimensional array by one or more
dimensions.