Niranjan
Niranjan

Reputation: 43

how to print array object data into array data in php codeigniter

I have array object data coming below format.I want to echo data as i given example html data. I tried different array function,finally not get result can you please check and give me solution please.My out put data is below format.

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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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] => [email protected]
                [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
            )

    )

Here am passing one dynamic variable is $selectedRoom="2". Means in the Object Array room_no is 2 total 4 arrays available with room_no 2.SO my required output is below.

Total Adult : 2

Total Child : 2

Number of Names : Here total 4 first_name will come from room_no='2' array. Here total 4 arrays is there room_no='2'. So i need 4 names will come here.

Note: If dynamic variable is $selectedRoom="1" then total 2 arrays will come with room_no="1",based on data will come.

I tried below code.

$array=json_decode(json_encode($passenger_info), True);
in_array($SelectedRoom, $array)

Upvotes: 1

Views: 476

Answers (2)

Ray A
Ray A

Reputation: 1351

You are doing it right, there is nothing wrong in your code for decoding the object. now, use this code to search, since in_array will not work in the multidimensional arrays, you can use this code

<?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.", ";
 }
?>

It should give you the result you wanted.

Upvotes: 1

Nigel Ren
Nigel Ren

Reputation: 57121

You don't need to encode and decode the data, just use the objects as they are. You then use something like $passenger->passenger_type to access the data.

This is a quick example which extracts the people for the rooms and then uses count() to display the totals. You also have all of the other data in the two arrays in case you need more details...

$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;

Upvotes: 0

Related Questions