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 }
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}