Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi I've got a bit of a problem and I can't seem to find the answer. I have an array with arrays in it and want to group these sub arrays into groups based on the same value for a field. This is my code and allows me to sort my arrays based on the arrays inside values. How do I group the result based on shared values? I probably won't know all the values as some are based on date and I want to for instance group all per day/month

if($filter == "ORDER BY TODO_REF DESC"){
$type_sort = 0;
};
if($filter == "ORDER BY TODO_PRIO DESC"){
$type_sort = 2;
};
if($filter == "ORDER BY TODO_DEAD DESC"){
$type_sort = 3;
};

function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;
}

 aasort($test_array, $type_sort);
print_r($test_array);

Current output:

priority
Array
 (

[3] => Array
    (
        [0] => 2
        [1] => sdfsdgdfgdfgdfg
        [2] => 3
        [3] => 2013-05-30 13:53:23
    )

[2] => Array
    (
        [0] => 1
        [1] => This must also be done
        [2] => 4
        [3] => 2013-03-28 12:13:34
    )

[1] => Array
    (
        [0] => 1
        [1] => testing this show me 2
        [2] => 5
        [3] => 2029-02-23 17:27:20
    )

[0] => Array
    (
        [0] => 1
        [1] => Do this task and make sure it gets done
        [2] => 5
        [3] => 2013-06-28 12:12:41
    )

)

What I want would be something like this, where they are split into seperate arrays based on sub array key 2:

Array
(
[3] => Array
    (
        [0] => 2
        [1] => sdfsdgdfgdfgdfg
        [2] => 3
        [3] => 2013-05-30 13:53:23
    )

)
Array
(
[2] => Array
    (
        [0] => 1
        [1] => This must also be done
        [2] => 4
        [3] => 2013-03-28 12:13:34
    )

)
Array
(
[1] => Array
    (
        [0] => 1
        [1] => testing this show me 2
        [2] => 5
        [3] => 2029-02-23 17:27:20
    )

[0] => Array
    (
        [0] => 1
        [1] => Do this task and make sure it gets done
        [2] => 5
        [3] => 2013-06-28 12:12:41
    )

)
share|improve this question

2 Answers 2

$array_1 = array();
$array_2 = array();
foreach($test_array as $item) {
  if($item[0] == 1) {
    $array_1[] = $item;
  } elseif($item[0] == 2) {
    $array_2[] = $item;
  }
}
$final_array = array($array_1, $array_2);

There must be more optimized codes, but this should serve your needs.

share|improve this answer
    
So I understand that what this will do is make 2 arrays. When I print_r $test_array[0]; I get an array but why does that array equal 1? Also what this does is place it in two arrays but doesn't seem to do this on the sub arrays value? Also when I use dates it will need to dynamicly add more arrays as there will be many more values to group by. –  Jake Rowsell Mar 5 '13 at 12:15
    
then you have to use auto-generated $array[$i] instead of $array_1 and $array_2 –  Raptor Mar 5 '13 at 17:15

array_multisort() is what you're after it will allow you to sort primary array by sub array and maintain the key value link

I use the below for sorting a rather hefty multi-dimensional array based off sort order arrows asc/desc on a table with many columns and sortable columns. Its abit of a mess but you should be able to follow it.

if (strlen($_GET['col'])<1) {
                foreach ($dataarray as $key => $row) {
                    $spend[$key]  = $row[6];
                }
                array_multisort($spend, SORT_DESC, $dataarray);
            } else if ($_GET['col']=="spend"){
                foreach ($dataarray as $key => $row) {
                    $spend[$key]  = $row[3];
                }
                if ($_GET['order']=="asc") {
                    array_multisort($spend, SORT_ASC, $dataarray);
                } else {
                    array_multisort($spend, SORT_DESC, $dataarray);
                }
            } else if ($_GET['col']=="name"){
                foreach ($dataarray as $key => $row) {
                    $name[$key]  = $row[1];
                }
                if ($_GET['order']=="asc") {
                    array_multisort($name, SORT_ASC, $dataarray);
                } else {
                    array_multisort($name, SORT_DESC, $dataarray);
                }
            } else if ($_GET['col']=="avg"){
                foreach ($dataarray as $key => $row) {
                    $avg[$key]  = $row[4];
                }
                if ($_GET['order']=="asc") {
                    array_multisort($avg, SORT_ASC, $dataarray);
                } else {
                    array_multisort($avg, SORT_DESC, $dataarray);
                }
            } else if ($_GET['col']=="sites"){
                foreach ($dataarray as $key => $row) {
                    $sites[$key]  = $row[5];
                }
                if ($_GET['order']=="asc") {
                    array_multisort($sites, SORT_ASC, $dataarray);
                } else {
                    array_multisort($sites, SORT_DESC, $dataarray);
                }
            } else if ($_GET['col']=="rate"){
                foreach ($dataarray as $key => $row) {
                    $rate[$key]  = $row[6];
                }
                if ($_GET['order']=="asc") {
                    array_multisort($rate, SORT_ASC, $dataarray);
                } else {
                    array_multisort($rate, SORT_DESC, $dataarray);
                }
            } else {
                foreach ($dataarray as $key => $row) {
                    $spend[$key]  = $row[2];
                }
                array_multisort($spend, SORT_DESC, $dataarray);
            }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.