group array php by key
private function group_by($array, $key) { $return = array(); foreach($array as $val) { $return[$val->$key][] = $val; // ou para gettype($val) = array // $return[$val[$key]][] = $val; } return $return; }
group array php by key
private function group_by($array, $key) { $return = array(); foreach($array as $val) { $return[$val->$key][] = $val; // ou para gettype($val) = array // $return[$val[$key]][] = $val; } return $return; }
php array group by key
$arrRoom[] = array("RoomCode" => "Deluxe", "Rates" => array ( array( "BoardCode" => "RO", "Price" => 100) )); $arrRoom[] = array("RoomCode" => "Standard", "Rates" => array ( array( "BoardCode" => "RO", "Price" => 100) )); $arrRoom[] = array("RoomCode" => "Deluxe", "Rates" => array (array( "BoardCode" => "RO", "Price" => 200) )); foreach($arrRoom as $room) { foreach($room['Rates'] as $rates) { $nRooms[$room['RoomCode']][$rates['BoardCode']][] = array("RoomCode" => $room['RoomCode'], "MealCode" => $rates['BoardCode'], "Price" => $rates['Price']); } } echo "\n ==== Output in Json Format ==== \n"; { "Deluxe": { "RO": [ { "RoomCode": "Deluxe", "MealCode": "RO", "Price": 100 }, { "RoomCode": "Deluxe", "MealCode": "RO", "Price": 200 } ] }, "Standard": { "RO": [ { "RoomCode": "Standard", "MealCode": "RO", "Price": 100 } ] } }
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us