如何在php codeigniter中将数组对象数据打印为数组数据

时间:2018-12-09 06:52:53

标签: php codeigniter-3

我有以下格式的数组对象数据。我想回显数据,就像给出的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中的均值是room_no 2.总共2个可用的4个数组 输出如下。

成人总数: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)

2 个答案:

答案 0 :(得分:1)

您做得对,对您的对象进行解码的代码没有错。 现在,使用此代码进行搜索,因为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.", ";
 }
?>

它应该给您想要的结果。

答案 1 :(得分:0)

您不需要编码和解码数据,只需按原样使用对象即可。然后,您使用$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;