[Solved] find array key is equal to value in specific array?


Kindly see below code to access multidimensional array with for each loop and subsequently check the elements with switch case. :

$passenger_info = array(0=> array("room_no"=>1,"passenger_type"=>"adult"), 1=>array("room_no"=>1,"passenger_type"=>"children"), 2=> array("room_no"=>1,"passenger_type"=>"adult"), 3=> array("room_no"=>2,"passenger_type"=>"children"), 4=> array("room_no"=>2,"passenger_type"=>"adult"));
echo "<pre>";
print_r($passenger_info);
echo "</pre>";


echo "<hr>";
$selected_room = 2;

$adult_count = 0;
$child_count = 0;
foreach($passenger_info as $key => $value) {
    if($value["room_no"] == $selected_room){
        switch ($value["passenger_type"]){
            case "adult":
                $adult_count++;
                break;
            case "children":
                $child_count++;
                break;
            default:
                break;
        }

    }

}

echo "Adults : ".$adult_count;
echo "<br>";
echo "Children : ".$child_count;

0

solved find array key is equal to value in specific array?