1
2$array = array(
3 array(
4 "name"=>"Urus",
5 "type"=>"SUV",
6 "brand"=>"Lamborghini"
7 ),
8 array(
9 "name"=>"Cayenne",
10 "type"=>"SUV",
11 "brand"=>"Porsche"
12 ),
13 array(
14 "name"=>"Panamera",
15 "type"=>"Car",
16 "brand"=>"Porsche"
17 ),
18);
19
20//filter by brand
21$arrayFilterQty = array_count_values(array_column($array, "brand"));
22
23/*
24Array
25(
26 [Lamborghini] => 1
27 [Porsche] => 2
28)
29*/
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}