Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

php - find array key is equal to value in specific array?

Hi All already i asked one question regarding array based key equal to value in array. Here my previous link please check enter link description here

If i follow the above link am not getting correct data. in_array finding all columns , but i want to find in room_no only one column in a array. If i use in_array(1,$singleArray) it will finding all columns in array,i need only key is room_no equal to 1.

Array
(
    [0] => Array
        (
            [pass_id] => 13087
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R1 F1
            [middle_name] => 
            [last_name] => R1 LI
            [gender] => 
            [child_age] => 
            [room_no] => 1
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15037
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-19 14:40:33
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [1] => Array
        (
            [pass_id] => 13088
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R1 F2
            [middle_name] => 
            [last_name] => R1 L2
            [gender] => 
            [child_age] => 
            [room_no] => 1
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 0
            [modify_date] => 
            [srm_pax_id] => 15038
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-11 12:00:35
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [2] => Array
        (
            [pass_id] => 13089
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R2 F1
            [middle_name] => 
            [last_name] => R2 L1
            [gender] => 
            [child_age] => 
            [room_no] => 2
            [zip_code] => 
            [state] => 1
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15039
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-11 12:00:35
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [3] => Array
        (
            [pass_id] => 13090
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Ms.
            [first_name] => R2 F2
            [middle_name] => 
            [last_name] => R2 L2
            [gender] => 
            [child_age] => 
            [room_no] => 2
            [zip_code] => 
            [state] => 1
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15040
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-19 14:40:38
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [4] => Array
        (
            [pass_id] => 13091
            [AL_RefNo] => H181200000061
            [passenger_type] => child
            [title] => Miss.
            [first_name] => R2 C1
            [middle_name] => 
            [last_name] => R2 CL1
            [gender] => 
            [child_age] => 10
            [room_no] => 2
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 
            [modify_date] => 
            [srm_pax_id] => 15041
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-11 12:00:35
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [5] => Array
        (
            [pass_id] => 13092
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R3 F1
            [middle_name] => 
            [last_name] => R3 L1
            [gender] => 
            [child_age] => 
            [room_no] => 3
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15042
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-11 12:00:35
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [6] => Array
        (
            [pass_id] => 13093
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Ms.
            [first_name] => R3 F2
            [middle_name] => 
            [last_name] => R3 L2
            [gender] => 
            [child_age] => 
            [room_no] => 3
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15043
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-19 14:46:17
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [7] => Array
        (
            [pass_id] => 13094
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R4 F1
            [middle_name] => 
            [last_name] => R4 L1
            [gender] => 
            [child_age] => 
            [room_no] => 4
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15044
            [PAX_NAME] => 
            [CITY] => 
            [HOTEL_NAME] => 
            [COUNTRY_NAME] => 
            [LPO_NUMBER] => 
            [LPO_DATE] => 
            [LPO_AMOUNT] => 
            [LPO_AMOUNT_DIFFERENCE] => 
            [LINE_SEQUENCE] => 
            [PROJECT_ID] => 
            [TRAVEL_TYPE] => 
            [OTHER_ID] => 
            [REMARKS] => 
            [REMARKS1] => 
            [updated_on] => 2018-12-19 14:46:25
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [8] => Array
        (
            [pass_id] => 13095
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Ms.
            [first_name] =&gt

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...