查找数组键等于特定数组中的值?

时间:2018-12-19 11:01:54

标签: php

大家好,我已经问过一个有关基于数组的键等于数组中的值的问题。 这是我之前的链接,请检查输入链接描述 here

如果我遵循上述链接,则无法获取正确的数据。 in_array查找所有列,但我想在room_no中找到数组中只有一列。 如果我使用in_array(1,$ singleArray)它将找到数组中的所有列,我只需要key是room_no等于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] => R4 F2
            [middle_name] => 
            [last_name] => R4 L2
            [gender] => 
            [child_age] => 
            [room_no] => 4
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 0
            [modify_date] => 
            [srm_pax_id] => 15045
            [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
        )

    [9] => Array
        (
            [pass_id] => 13096
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Mr.
            [first_name] => R5 F1
            [middle_name] => 
            [last_name] => R5 L1
            [gender] => 
            [child_age] => 
            [room_no] => 5
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 1
            [modify_date] => 
            [srm_pax_id] => 15046
            [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-18 17:54:33
            [actual_hotel_confirmation_number] => 
            [is_delete] => 0
            [modify_refno] => H181200000061_1
        )

    [10] => Array
        (
            [pass_id] => 13097
            [AL_RefNo] => H181200000061
            [passenger_type] => adult
            [title] => Ms.
            [first_name] => R5 F2
            [middle_name] => 
            [last_name] => R5 L2
            [gender] => 
            [child_age] => 
            [room_no] => 5
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 0
            [modify_date] => 
            [srm_pax_id] => 15047
            [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
        )

    [11] => Array
        (
            [pass_id] => 13098
            [AL_RefNo] => H181200000061
            [passenger_type] => child
            [title] => Master.
            [first_name] => R5 CF1
            [middle_name] => 
            [last_name] => R5 CL1
            [gender] => 
            [child_age] => 9
            [room_no] => 5
            [zip_code] => 
            [state] => 
            [mobile] => 
            [email] => 
            [address] => 
            [country] => 
            [special_request] => 
            [HQRequest] => 
            [lead] => 
            [modify_date] => 
            [srm_pax_id] => 15048
            [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
        )

)

如果您在数组[room_no]中发现了该数组[0]中2次的上述输出,则为数组1。如果我使用in_array(1,$myArrayOutPut){ },它将使用array [3],array [4]状态键也为1。因此,将键为1的所有数组都作为键,但是我需要room_no为1

1 个答案:

答案 0 :(得分:0)

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;