This question already has answers here:
PHP re-order array of month names
(5 answers)
Closed 8 years ago.
I'm trying to reorganize my array orderned by date.
For example this is my array:
Array(
[0] => august
[1] => july
[2] => october
[3] => september
)
How can i reorganize it chronologically, so it would become this:
Array(
[0] => july
[1] => august
[2] => september
[3] => october
)
Use date_parse to parse the month name and convert it to the corresponding number and then use ksort to sort them.
$myArray = array('august', 'july', 'october', 'september');
foreach($myArray as $value) {
$m = date_parse($value);
$output[$m['month']] = ucfirst($value);
}
ksort($output);
print_r($output);
Output:
Array
(
[7] => July
[8] => August
[9] => September
[10] => October
)
Source: #12424968
Demo!
This should work for any date that strtotime can parse
function cmp($a, $b)
{
$a_time = strtotime($a);
$b_time = strtotime($b);
if ($a_time == $b_time) {
return 0;
}
return ($a_time < $b_time) ? -1 : 1;
}
// Array to be sorted
$array = array('november', 'august', 'december', 'february');
print_r($array);
// Sort and print the resulting array
usort($array, 'cmp');
print_r($array);
Output:
Array
(
[0] => november
[1] => august
[2] => december
[3] => february
)
Array
(
[0] => february
[1] => august
[2] => november
[3] => december
)
Related
I have an array like this below,
Array
(
[2] => Array
(
[id] => 75
[program] => Apr 2020-Nov 2020
)
[1] => Array
(
[id] => 73
[program] => Feb 2016-Aug 2020
)
[0] => Array
(
[id] => 72
[program] => May 2020-Dec 2020
)
)
The resultant array should be
Array
(
[1] => Array
(
[id] => 73
[current_program] => Feb 2016-Aug 2020
)
[2] => Array
(
[id] => 75
[current_program] => Apr 2020-Nov 2020
)
[0] => Array
(
[id] => 72
[current_program] => May 2020-Dec 2020
)
)
It should sort based on the year. I have tried to achieve by "strnatcasecmp", but the array is sorting by alphabet not by the numeric value in it
usort($programp, function($a, $b) {
return strnatcasecmp($a['program'], $b['program']);
});
Any help would be appreciated!
Thanks
You need to convert the first month and year into a timestamp, sort that thereby sorting the original:
foreach($programp as $values) {
$starts[] = strtotime(explode('-', $values['program'])[0]);
}
array_multisort($starts, $programp);
Before sorting the $starts array would look like this, easy to sort:
Array
(
[0] => 1585692000
[1] => 1454281200
[2] => 1588284000
)
You probably want some error checking to make sure $values['program'] is not empty etc...
//Sort indexes so the keys are 0,1,2 instead of 1,2,0
//This is important when sorting down below with asort()
$arr = array_values($arr);
//Go through every "program" (dateinterval in your array)
//and make a new array in the format year-monthnr
//Date parse returns the number of jan(1),feb(2),mar(3) etc..
$year_month = [];
foreach(array_column($arr,'program') as $key => $item) {
$year = explode(' ', explode('-',$item)[0])[1];
$month = date_parse(explode(' ', explode('-',$item)[0])[0])['month'];
$year_month[] = $year . '-' . $month;
}
//Sort with mainted indexes (keys)
asort($year_month);
//Create new array "mapped" keys to the original array $arr
$new_arr = [];
foreach($year_month as $key=>$item) {
$new_arr[] = $arr[$key];
}
Output of $new_arr would be:
Array
(
[0] => Array
(
[id] => 73
[program] => Feb 2016-Aug 2020
)
[1] => Array
(
[id] => 75
[program] => Apr 2020-Nov 2020
)
[2] => Array
(
[id] => 72
[program] => May 2020-Dec 2020
)
)
I have an array like the following:
$f= array("Sunday", "Monday","Tuesday","Wednesday", "Thursday","Friday", "Saturday");
I have run a foreach loop for $f and print date. Result is:
Array ( [0] => Sunday [1] => Monday [2] => Tuesday [3] => Wednesday [4] => Thursday [5] => Friday [6] => Saturday )
but I want if it is Wednesday today, then the output will be:
Array ( [0] => Wednesday [1] => Thursday [2] => Friday [3] => Saturday [4] => Sunday [5] => Monday [6] => Tuesday )
Here is my code:
$f= array("Sunday", "Monday","Tuesday","Wednesday", "Thursday","Friday", "Saturday");
foreach ($f as $value) {
if($value == date('l')){
$date[] = $value;
}
}
print_r($date);
it return me the following result:
Array ( [0] => Thursday [1] => Thursday [2] => Thursday [3] => Thursday [4] => Thursday [5] => Thursday [6] => Thursday )
You can do it by native php functions:
$i = array_search(date('l'), $f);
$date = array_merge(array_splice($f, $i), $f);
array_search finds today item in the array, array_splice removes tail of the array and returns it, array_merge combines that arrays in the proper order
demo on eval.in
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.
I have a large array of booking dates and amount, like so :
Array
(
[0] => Array
(
[date] => 2014-04-04
[total] => 30.00
)
[1] => Array
(
[date] => 2014-04-05
[total] => 47.00
)
[9998] => Array
(
[date] => 2014-08-21
[total] => 52.00
)
... ++ a lot of dates associated to numbers.
and process it to get monthly reports :
$months = array();
foreach($myarray as $k=>$v) {
list($y,$m) = explode("-",$v['date']);
$months[$y."-".$m][] = $v['total'];
}
which gives me a nice array :
Array
(
[2014-04] => Array <-- Every amount made in April
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
...
)
[2014-05] => Array <-- Every amount made in May
(
[0] => 68.00
[1] => 42.00
....
)...
However I'm trying to find a way the same thing but starting from a specific day, say 5 for example, in order to get (dates labelled for clarity):
Array
(
[from January 5 to February 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)
[from February 5 to March 4] => Array
(
[0] => 30.00
[1] => 47.00
[2] => 47.00
)...
So every amount made each month, but starting from the 5th of each month.
Something like this should work. I hate saying should, but I don't have the data to test with. Let me know if anything's wrong:
$months = array();
foreach($months as $k => $v) {
list($y, $m, $d) = explode("-", $v['date']);
if($d < 5) {
if($m == 1) {
$y--;
$m = 12;
} else {
$m--;
}
}
$months[$y . "-" . $m][] = $v['total'];
}
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);