0

I'm having a bit of trouble sorting an array by sub-sub array values. I've tried some things like usort but it doesn't seem to work. I have a multidimensional array called $array_1 which is made dynamicly based on search criteria (for instance a deadline date, priority number, group number etc.) which creates a subarray per value that is the same. i.e. it'll make a sub-array for all the sub-sub arrays that have group->1, another sub-array for group->2. Now I want to sort the sub-arrays by for instance date, which should keep the sub-arrays clustered by group(1 and 2).

edit: I'm now using an array sorting function and I think the problem is as follows: I can use values but not variables in the following function:

 foreach($array_1 as &$arr){
usort($arr,"cmp");
}

function cmp($a, $b) {
    if ($a[2] == $b[2]) {
            return 0;
    }
    return ($a[2] < $b[2]) ? -1 : 1;
}
echo $type;
print_r($array_1);

if I replace the number 2 with a variable which is also 2 and have check with is_numeric the function doesn't work. Why is this?

Jake Rowsell
  • 466
  • 5
  • 16
  • foreach works on a _copy_ of the array – if you want to manipulate the actual array inside a foreach loop, you have to pass it via reference. http://www.php.net/manual/en/control-structures.foreach.php – CBroe Mar 18 '13 at 10:34
  • Thanks for your reply, so to make it a reference variable I change $sub to &$sub right? But this still doesn't work.. – Jake Rowsell Mar 18 '13 at 10:44
  • And _“doesn’t work”_ is _still_ not a valid problem/error description. – CBroe Mar 18 '13 at 10:45
  • Sorry by doesn't work I meant to say the sorting by sub-sub array value is not occurring. I get the same as when I didn't make it reference variables. edit: When I want to sort by sub-sub array value nothing changes at all. I'll just update the post to show some more of the page – Jake Rowsell Mar 18 '13 at 10:51
  • Please give code that is easy to try out by copy&paste – use `var_export` to display your data arrays in code form. – CBroe Mar 18 '13 at 10:59
  • I've checked the thread and although it similar I think my issue is different. I've updated the question so perhaps now you may see something else I am doing wrong? – Jake Rowsell Mar 18 '13 at 13:31

0 Answers0