php array group by key

Solutions on MaxInterview for php array group by key by the best coders in the world

showing results for - "php array group by key"
Lucas
23 May 2020
1  private function group_by($array, $key) {
2      $return = array();
3      
4      foreach($array as $val) {
5          $return[$val->$key][] = $val; 
6	  //  ou para gettype($val) = array
7	  //  $return[$val[$key]][] = $val; 
8      }
9      return $return;
10  }
Matteo
17 Aug 2020
1$arrRoom[] = array("RoomCode" => "Deluxe",
2                    "Rates" => array ( array(
3                        "BoardCode" => "RO",
4                        "Price" => 100)
5                    ));
6$arrRoom[] = array("RoomCode" => "Standard",
7                    "Rates" => array ( array(
8                        "BoardCode" => "RO",
9                        "Price" => 100)                        
10                    ));                    
11$arrRoom[] = array("RoomCode" => "Deluxe",
12                    "Rates" => array (array(
13                        "BoardCode" => "RO",
14                        "Price" => 200)
15                    ));
16
17foreach($arrRoom as $room)
18{    
19    foreach($room['Rates'] as $rates)
20    {        
21        $nRooms[$room['RoomCode']][$rates['BoardCode']][] = array("RoomCode" => $room['RoomCode'],
22                                                            "MealCode" => $rates['BoardCode'],
23                                                            "Price" => $rates['Price']);
24    }
25}
26echo "\n ==== Output in Json Format ==== \n";
27{
28    "Deluxe": {
29        "RO": [
30            {
31                "RoomCode": "Deluxe",
32                "MealCode": "RO",
33                "Price": 100
34            },
35            {
36                "RoomCode": "Deluxe",
37                "MealCode": "RO",
38                "Price": 200
39            }
40        ]
41    },
42    "Standard": {
43        "RO": [
44            {
45                "RoomCode": "Standard",
46                "MealCode": "RO",
47                "Price": 100
48            }
49        ]
50    }
51}
queries leading to this page
php group array by valuegroup by key php groupbyelements in an array phpgroup elements in array phpgroup value in array phpphp grouping array of array based on value in the arrayphp group array by key valuearray group array phparray group by keyphp group associative array by valuegrouping array of array based on value in the arrayhow to group array items phphow to foreach of group an arrayhow to array groupby in phpcommon array methods phpgroup array by values phpgroup arrays by key phpgroup array by value phpgroup array by key phpphp group by array valuearray functions in phpphp group by with 2 key in arrayarray php group by keygroup arrays by valuegtoup array of zero phpphp group by array keygroup array php by valuegroup values by keys phpphp grop by keygroup items in a array using phpphp 2b group array object byphp group array by same valuephp group array by valuesphp group array items base on key valuephp how to group arrayphp group array itemsgroup array php by keyphp group data in arraygroup element in array phpphp group by ketygroup array based on there similer value in phpgroup array on certain values in phpphp array 28 29 methodsgroup array based on key phplaravel array group by keygroup array by keyarray group by key from array phpphp array group byphp group array elements by valuegroup array phpgroup by key array phpassignment array 2ccontruct array 2cfunctions returning array in phpphp group array of array ased on valuegroup array collection phphow to array group by array in phpgroup array contentgroup array by phpphp group array by keygroup array elements by key in phpcreate group based array phpgroup array per key laravelarray group by value phpphp group array object byphp group array by vauehow to group array of objects by key in phparray group by key phpgroup array elements by value phpgroup by key value pair array in phpphp map group into arraygroup by array key value in phpphp array group by fieldgroup array of arrays bsed on id phpphp array group by value and get quantityphp array group by keyphp group array by multiple valuesgroup by array key value in php arraymaparray functions list keysarray group by array key in phpgroupby an element in array phpgrouping array php by keygroup 2 keys with value from an array phpphp array group by valuearray group by index key phpphp group array by elementsgroup array based on value phpgrouping key array phpgroup array value phpphp group arraygroup by array key phpgroup array of objects by key phparray same key group bygroup array by key php needphp array group by key valuearay group by key phpphp group keys to arrayphp array key value groupphp group by keyphp associative array methodsphp group array by multiple keysphp array group by key