Jak pogrupować wielowymiarową tablicę według konkretnej wartości podrzędnej?

Mam wielowymiarową tablicę i próbuję pogrupować je według wartości w określonej kolumnie.

Próbuję pogrupować je według level, ale nie będę znać wcześniej poziomu. Nie mogę więc umieścić go w pętli for i powiedzieć while $i < 7, Ponieważ Nie będę wiedział, że 7 jest maksymalną wartością dla klucza poziomu i szczerze mówiąc, nie jestem pewien, czy tak musiałbym to zrobić, nawet gdybym to zrobił...

Array (
   [0] => Array (
          [cust] => XT8900
          [type] => standard
          [level] => 1
          )
   [1] => Array (
          [cust] => XT8944
          [type] => standard
          [level] => 1
          )
   [2] => Array (
          [cust] => XT8922
          [type] => premier
          [level] => 3
          )
   [3] => Array (
          [cust] => XT8816
          [type] => permier
          [level] => 3
          )
   [4] => Array (
          [cust] => XT7434
          [type] => standard
          [level] => 7
          )
)

Co mam nadzieję wyprodukować:

Array (

   [1] => Array (
          [0] => Array (
                    [cust] => XT8900
                    [type] => standard
                    )
          [1] => Array (
                    [cust] => XT8944
                    [type] => standard
                    )
          )

   [3] => Array (
          [2] => Array (
                 [cust] => XT8922
                 [type] => premier
                 )

          [3] => Array (
                 [cust] => XT8816
                 [type] => permier
                 )
          )

   [7] => Array (
          [4] => Array (
                 [cust] => XT7434
                 [type] => standard
                 )
          )
)
Author: mickmackusa, 2010-02-03

6 answers

Musisz pogrupować je według level first

Użyj foreach aby zapętlić do tablicy sprawdź, czy poziom jest taki sam z poprzednim elementem, a następnie Grupuj go z tą tablicą

  $templevel=0;   

  $newkey=0;

  $grouparr[$templevel]="";

  foreach ($items as $key => $val) {
   if ($templevel==$val['level']){
     $grouparr[$templevel][$newkey]=$val;
   } else {
     $grouparr[$val['level']][$newkey]=$val;
   }
     $newkey++;       
  }
print($grouparr);

Wyjście print ($grouparr); wyświetli się tak, jak oczekiwałeś

Możesz również spróbować

print($grouparr[7]);

Wyświetli

 [7] => Array (
      [4] => Array (
             [cust] => XT7434
             [type] => standard
             )
      )

Lub

print($grouparr[3]);

Wyświetli

[3] => Array (
      [2] => Array (
             [cust] => XT8922
             [type] => premier
             )

      [3] => Array (
             [cust] => XT8816
             [type] => permier
             )
      )
 9
Author: Gerard Banasig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-03 05:40:54

Najlepszym sposobem, jeśli masz kontrolę nad budowaniem początkowej tablicy, jest po prostu skonfigurowanie takich rzeczy na początku, gdy dodajesz wpisy.

If not then built a temporary array to sort:

foreach ($input_arr as $key => &$entry) {
    $level_arr[$entry['level']][$key] = $entry;
}

Zostawia ci formularz, który chciałeś i wszystko, o czym się razem mówi.

Zbuduj tablicę w ten sposób, chociaż jeśli to w ogóle możliwe.

 35
Author: Mike,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-03 05:33:45
function group_assoc($array, $key) {
    $return = array();
    foreach($array as $v) {
        $return[$v[$key]][] = $v;
    }
    return $return;
}

//Group the requests by their account_id
$account_requests = group_assoc($requests, 'account_id');
 6
Author: Stephen Bugs Kamenar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-27 15:09:30

Oto rozwiązanie, na którym wylądowałem dla identycznego problemu, zawiniętego jako funkcja:

function arraySort($input,$sortkey){
  foreach ($input as $key=>$val) $output[$val[$sortkey]][]=$val;
  return $output;
}

Aby posortować $myarray według klucza o nazwie "level" po prostu zrób to:

$myArray = arraySort($myArray,'level');

Lub jeśli nie chcesz tego jako funkcji, tylko do jednorazowego użycia, utworzyłoby to $mynewarray z $myArray zgrupowane według klucza 'level'

foreach ($myArray as $key=>$val) $myNewArray[$val['level']][]=$val;
 5
Author: Kevin Hagerty,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-15 14:54:28
  $result = array();
    foreach ($yourArrayList as $data) {
        $id = $data['level'];
        if (isset($result[$id])) {
            $result[$id][] = $data;
        } else {
            $result[$id] = array($data);
        }
    }
 1
Author: user4155960,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-18 06:13:17
function _group_by($array,$key,$keyName)
    {
          $return = array();

          foreach($array as $val) {
              $return[$keyName.$val[$key]][] = $val;
          }
         return $return;

    }  //end of function
 -1
Author: user6394097,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-28 10:09:54