php group array by multiple values code example

Example 1: 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;
  }

Example 2: in array php multiple values

$haystack = array(...);

$target = array('foo', 'bar');

if(count(array_intersect($haystack, $target)) == count($target)){
    // all of $target is in $haystack
}

if(count(array_intersect($haystack, $target)) > 0){
    // at least one of $target is in $haystack
}

Example 3: 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
            }
        ]
    }
}