1

I have an array with the following structure:

for ($i = 0; $i < SOME_NUMBER; $i++) {
   $arr[$i][] = $info_1;
   $arr[$i][] = $info_2;
   $arr[$i][] = $info_3;
   $arr[$i][] = $DATE;
   $arr[$i][] = $info_4;
}

I need this array to be sorted by $DATE. Now i can not change the structure of the array, i cant change keys. Is it possible to sort $arr with $DATE

Example:

 after running the loop for 2 times $arr is equal to:

 $arr[0][0] = 'Some Info 1.1';
 $arr[0][1] = 'Some Info 1.2';
 $arr[0][2] = 'Some Info 1.3';
 $arr[0][3] = '05-08-2010';
 $arr[0][4] = 'Some Info 1.4';

 $arr[1][0] = 'Some Info 2.1';
 $arr[1][1] = 'Some Info 2.2';
 $arr[1][2] = 'Some Info 2.3';
 $arr[1][3] = '01-08-2011';
 $arr[1][4] = 'Some Info 2.4';

the expected output would be:

 $arr[0][0] = 'Some Info 2.1';
 $arr[0][1] = 'Some Info 2.2';
 $arr[0][2] = 'Some Info 2.3';
 $arr[0][3] = '01-08-2011';
 $arr[0][4] = 'Some Info 2.4';

 $arr[1][0] = 'Some Info 1.1';
 $arr[1][1] = 'Some Info 1.2';
 $arr[1][2] = 'Some Info 1.3';
 $arr[1][3] = '05-08-2010';
 $arr[1][4] = 'Some Info 1.4';

using array_sort didnt work.

2
  • nvm i dont know why i wrote array_keys, i meant array_sort Commented Mar 29, 2013 at 14:42
  • Even so, stack is about helping you with your code not writing it for you; you have not shown us what you've tried that failed. 'using array_sort' does not tell us what you did, only that you used a function somehow. Edit your question above. Commented Mar 29, 2013 at 14:44

2 Answers 2

4

Sorting on the fourth index of each array item, based on a strtotime() comparison:

uasort($arr, function($a, $b) {
    return strtotime($a[3]) - strtotime($b[3]);
});
Sign up to request clarification or add additional context in comments.

1 Comment

That was Brilliant! Wish I could +5 rather than +1
2

You should use php function usort() that sorts an array using a custom function. Here is how to do it at a glance.

Initialize your array:

 $arr[0][0] = 'Some Info 1.1';
 $arr[0][1] = 'Some Info 1.2';
 $arr[0][2] = 'Some Info 1.3';
 $arr[0][3] = '05-08-2010';
 $arr[0][4] = 'Some Info 1.4';

 $arr[1][0] = 'Some Info 2.1';
 $arr[1][1] = 'Some Info 2.2';
 $arr[1][2] = 'Some Info 2.3';
 $arr[1][3] = '01-08-2011';
 $arr[1][4] = 'Some Info 2.4';

Declare the comparison function that compares the two dates:

function cmp($a,$b){
 $d1 = date_parse($a[3]);
 $d2 = date_parse($b[3]);
 return  ($d1 < $d2) ? 1 : -1;
}

and finally call usort() using your comparison function.

usort($arr,"cmp");

Please note that the mentioned comparison function assumes that the date is always at the 3rd index. If you want more flexibility, customize the comparison function to find the date index before the actual comparison. Good luck!


More info:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.