我有以下格式的数组对象数据。我想回显数据,就像我给出的示例html数据一样。我尝试了不同的数组函数,最后没有得到结果,请检查并给我解决方案。我的输出数据低于格式。
echo "<pre>";print_r($passenger_info); exit();
Out put is below.
Array
(
[0] => stdClass Object
(
[pass_id] => 12815
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 1
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 1
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[1] => stdClass Object
(
[pass_id] => 12816
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 1
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[2] => stdClass Object
(
[pass_id] => 12817
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 2
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[3] => stdClass Object
(
[pass_id] => 12818
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 2
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[4] => stdClass Object
(
[pass_id] => 12819
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 3
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[5] => stdClass Object
(
[pass_id] => 12820
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 3
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[6] => stdClass Object
(
[pass_id] => 12821
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 4
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[7] => stdClass Object
(
[pass_id] => 12822
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 4
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[8] => stdClass Object
(
[pass_id] => 12823
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 5
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[9] => stdClass Object
(
[pass_id] => 12824
[AL_RefNo] => H181100000133
[passenger_type] => adult
[title] => Mr.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 5
[zip_code] =>
[state] =>
[mobile] => +971-9999999999
[email] => 9@gmail.com
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] => 0
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[10] => stdClass Object
(
[pass_id] => 12825
[AL_RefNo] => H181100000133
[passenger_type] => child
[title] => Miss.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 2
[zip_code] =>
[state] =>
[mobile] =>
[email] =>
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] =>
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[11] => stdClass Object
(
[pass_id] => 12826
[AL_RefNo] => H181100000133
[passenger_type] => child
[title] => Master.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] => 1
[room_no] => 2
[zip_code] =>
[state] =>
[mobile] =>
[email] =>
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] =>
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[12] => stdClass Object
(
[pass_id] => 12827
[AL_RefNo] => H181100000133
[passenger_type] => child
[title] => Miss.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] => 4
[room_no] => 4
[zip_code] =>
[state] =>
[mobile] =>
[email] =>
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] =>
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
[13] => stdClass Object
(
[pass_id] => 12828
[AL_RefNo] => H181100000133
[passenger_type] => child
[title] => Miss.
[first_name] => Chitta
[middle_name] =>
[last_name] => user
[gender] =>
[child_age] =>
[room_no] => 4
[zip_code] =>
[state] =>
[mobile] =>
[email] =>
[address] =>
[country] =>
[special_request] =>
[HQRequest] =>
[lead] =>
[modify_date] =>
[srm_pax_id] => 7072
[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-11-27 17:03:47
[actual_hotel_confirmation_number] =>
[is_delete] => 0
[modify_refno] => H181100000133_1
)
)
这里传递的一个动态变量是$selectedRoom="2"。意味着对象阵列中的room_no共有2个4个阵列可用,room_no为2。因此我需要输出低于。
成人总数:2
儿童总数:2
名称数量:这里总共有4个first_name将来自room_no='2'数组。这里总共有4个数组,room_no='2'。所以我需要4个名字来这里。
注意:如果动态变量是$selectedRoom="1",那么总共2个数组将带有room_no="1",根据数据而来。
我尝试了以下代码。
$array=json_decode(json_encode($passenger_info), True);
in_array($SelectedRoom, $array)
您做得对,对对象进行解码的代码没有任何错误。现在,使用此代码进行搜索,因为in_array
在多维数组中不起作用,所以可以使用此代码
<?php
$array=json_decode(json_encode($passenger_info), True);
$totalAdults = 0;
$totalChildren = 0;
$names = array();
foreach($array as $singleArray){
if($SelectedRoom == $singleArray['room_no']){
$names[] = $singleArray['first_name'].' '.$singleArray['middle_name'].' '.$singleArray['last_name'];
switch($singleArray['passenger_type']){
case 'adult':
$totalAdults +=1;
break;
case 'child':
$totalChildren +=1;
break;
}
}
echo "Total Adults: ".$totalAdults."<br/> Total Children: ".$totalChildren;
foreach($names as $name){
echo "Name: ".$name.", ";
}
?>
它应该会给你想要的结果。
您不需要对数据进行编码和解码,只需按原样使用对象即可。然后使用类似$passenger->passenger_type
的东西来访问数据。
这是一个快速示例,它提取房间的人员,然后使用count()
来显示总数。您还拥有两个数组中的所有其他数据,以备需要更多详细信息时使用。。。
$selectedRoom = 1;
$adults = [];
$children = [];
foreach ( $passenger_info as $passenger ) {
if ( $passenger->room_no == $selectedRoom ) {
if ( $passenger->passenger_type == "adult" ) {
$adults[] = $passenger;
}
else {
$children[] = $passenger;
}
}
}
echo "total adults = ".count($adults).PHP_EOL;
echo "total children = ".count($children).PHP_EOL;