group array php by key

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

showing results for - "group array php by key"
Antonella
29 Oct 2017
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  }
Salomé
03 Sep 2017
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 vauephp array group by key valuegroup array collection phpphp array group by valuearray group by key phpphp array key value groupgroup array of objects by key phpgroup array elements by key in phpphp group array by multiple valuesphp group array of array ased on valuehow to group array of objects by key in phparray php group by keyphp group by keyphp group keys to arrayphp grop by keycreate group based array phpgroup array contentgroup array by phparray group by key from array phpgroup array php by valuegroup array on certain values in phparay group by key phphow to array group by array in phpgroup arrays by key phpphp group array by key valuephp group data in arrayphp group array by multiple keyshow to group array items phpphp array 28 29 methodsgroup items in a array using phpgroup array php by keygroupby an element in array phpphp array group by fieldgroup array by key php needgroupbyelements in an array phpgroup values by keys phpgroup by array key value in phpphp array group byhow to foreach of group an arrayphp group array by elementsgroup array based on value phparray group by value phpassignment array 2ccontruct array 2cfunctions returning array in phparray group by array key in phpgroup by array key value in php arraymapgtoup array of zero phpphp group array by keygroup array by keygroup by key value pair array in phpphp associative array methodsarray functions list keysgrouping array of array based on value in the arraycommon array methods phpphp array group by keygroup value in array phpphp how to group arrayarray group by keyphp group array itemsgroup by key array phparray group array phpgrouping key array phpgroup array of arrays bsed on id phplaravel array group by keygroup by array key phpphp group array items base on key valuephp array group by value and get quantityphp 2b group array object bygroup array by key phpgroup by key php php group by array valuegrouping array php by keyphp grouping array of array based on value in the arraygroup array based on there similer value in phpphp group by ketyphp map group into arraygroup array based on key phpgroup array value phpgroup elements in array phpphp group by array keyarray group by index key phparray functions in phpgroup array by values phpgroup element in array phparray same key group bygroup array elements by value phpphp group array by valuesgroup 2 keys with value from an array phpphp group associative array by valuegroup array by value phpphp group array by same valuephp group array elements by valuephp group by with 2 key in arrayphp group arrayhow to array groupby in phpgroup array phpgroup array per key laravelgroup arrays by valuephp group array by valuephp group array object bygroup array php by key