3

In this array function to we want to get the hotel details. I used the following code to get the value. In that code I'm not getting proper result; some the hotels do not show the price. I provided the output as well.

Array
(
    [TestMode] => true
    [HotelAvailability] => Array
        (
            [0] => Array
                (
                    [Hotel] => Array
                        (
                            [Id] => 87649
                            [Name] => Asmali
                            [Region] => Array
                                (
                                    [Id] => 21303
                                    [Name] => Old City
                                )

                            [Type] => Hotel
                            [Stars] => 3
                            [Rank] => 1
                        )

                    [Result] => Array
                        (
                            [0] => Array
                                (
                                    [QuoteId] => 12297840-763
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1103598
                                                    [Text] => Double Economy
                                                )

                                            [MealType] => Array
                                                (
                                                    [Code] => 1000018
                                                    [Text] => Bed and breakfast
                                                )

                                            [SellingPrice] => Array
                                                (
                                                    [Currency] => INR
                                                    [Amount] => 2094.87
                                                    [Estimated] => false
                                                    [Converted] => false
                                                )

                                            [Guests] => Array
                                                (
                                                    [Adult] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [Id] => -452103032
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 1047.44
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [Id] => -452103033
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 1047.43
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                            [1] => Array
                                (
                                    [QuoteId] => 12297840-764
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1002196
                                                    [Text] => Double Standard
                                                )

                                            [MealType] => Array
                                                (
                                                    [Code] => 1000018
                                                    [Text] => Bed and breakfast
                                                )

                                            [SellingPrice] => Array
                                                (
                                                    [Currency] => INR
                                                    [Amount] => 2993.22
                                                    [Estimated] => false
                                                    [Converted] => false
                                                )

                                            [Guests] => Array
                                                (
                                                    [Adult] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [Id] => -452103034
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 1496.61
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [Id] => -452103035
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 1496.61
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                            [2] => Array
                                (
                                    [QuoteId] => 12297840-765
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1002210
                                                    [Text] => Double Deluxe
                                                )

                                            [MealType] => Array
                                                (
                                                    [Code] => 1000018
                                                    [Text] => Bed and breakfast
                                                )

                                            [SellingPrice] => Array
                                                (
                                                    [Currency] => INR
                                                    [Amount] => 4190.37
                                                    [Estimated] => false
                                                    [Converted] => false
                                                )

                                            [Guests] => Array
                                                (
                                                    [Adult] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [Id] => -452103036
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2095.18
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [Id] => -452103037
                                                                    [Forename] => Array
                                                                        (
                                                                        )

                                                                    [Surname] => Array
                                                                        (
                                                                        )

                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2095.19
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                            [3] => Array
                                (
                                    [QuoteId] => 12297840-329
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1286415
                                                    [Text] => Twin (Standard Twin)
                                                )

                                            [MealType] => Array
                                                (
                                                    [Code] => 1000030
                                                    [Text] => Full Breakfast
                                                )

                                            [SellingPrice] => Array
                                                (
                                                    [Currency] => INR
                                                    [Amount] => 5384.70
                                                    [Estimated] => false
                                                    [Converted] => false
                                                )

                                            [Guests] => Array
                                                (
                                                    [Adult] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [Id] => -113025469
                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2692.35
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [Id] => -113025470
                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2692.35
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                            [4] => Array
                                (
                                    [QuoteId] => 12297840-348
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1832194
                                                    [Text] => Double (Standard Twin)
                                                )

                                            [MealType] => Array
                                                (
                                                    [Code] => 1000030
                                                    [Text] => Full Breakfast
                                                )

                                            [SellingPrice] => Array
                                                (
                                                    [Currency] => INR
                                                    [Amount] => 5384.70
                                                    [Estimated] => false
                                                    [Converted] => false
                                                )

                                            [Guests] => Array
                                                (
                                                    [Adult] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [Id] => -113025507
                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2692.35
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                            [1] => Array
                                                                (
                                                                    [Id] => -113025508
                                                                    [SellingPrice] => Array
                                                                        (
                                                                            [Currency] => INR
                                                                            [Amount] => 2692.35
                                                                            [Estimated] => false
                                                                            [Converted] => false
                                                                        )

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                        )

                )

            [1] => Array
                (
                    [Hotel] => Array
                        (
                            [Id] => 1712021
                            [Name] => May Hotel
                            [Region] => Array
                                (
                                    [Id] => 21303
                                    [Name] => Old City
                                )

                            [Type] => Hotel
                            [Stars] => 4
                            [Rank] => 1
                        )

                    [Result] => Array
                        (
                            [QuoteId] => 12297840-899
                            [Room] => Array
                                (
                                    [RoomType] => Array
                                        (
                                            [Code] => 1002196
                                            [Text] => Double Standard
                                        )

                                    [MealType] => Array
                                        (
                                            [Code] => 1000018
                                            [Text] => Bed and breakfast
                                        )

                                    [SellingPrice] => Array
                                        (
                                            [Currency] => INR
                                            [Amount] => 2217.18
                                            [Estimated] => false
                                            [Converted] => false
                                        )

                                    [Guests] => Array
                                        (
                                            [Adult] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [Id] => -452103440
                                                            [Forename] => Array
                                                                (
                                                                )

                                                            [Surname] => Array
                                                                (
                                                                )

                                                            [SellingPrice] => Array
                                                                (
                                                                    [Currency] => INR
                                                                    [Amount] => 1108.59
                                                                    [Estimated] => false
                                                                    [Converted] => false
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [Id] => -452103441
                                                            [Forename] => Array
                                                                (
                                                                )

                                                            [Surname] => Array
                                                                (
                                                                )

                                                            [SellingPrice] => Array
                                                                (
                                                                    [Currency] => INR
                                                                    [Amount] => 1108.59
                                                                    [Estimated] => false
                                                                    [Converted] => false
                                                                )

                                                        )

                                                )

                                        )

                                    [Confirmation] => allocation
                                )

                        )

                )

Output :

87649 - Asmali: Old City 3 
1712021 - May Hotel: Old City 4 INR 2217.18

My code :

    foreach($array_data['HotelAvailability'] as $hotel) {
        $id = $hotel['Hotel']['Id'];
        $name = $hotel['Hotel']['Name'];
        $region = $hotel['Hotel']['Region']['Name'];
        $stars = $hotel['Hotel']['Stars'];
        $price = $hotel['Result']['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result']['Room']['SellingPrice']['Amount'] ;

       echo "$id - $name: $region $stars $price<br>\n"; 
   }

3 Answers 3

1

As you cann see Result have more than one subarray, if you need to have details related to first entry you can use its index 0

$price = $hotel['Result'][0]['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result'][0]['Room']['SellingPrice']['Amount'] ;

You can change index [0] with any result array you need, otherwise you can loop throw Result sub arrays and retrieve all details you need about them

foreach($array_data['HotelAvailability'] as $hotel) 
{
    $id = $hotel['Hotel']['Id'];
    $name = $hotel['Hotel']['Name'];
    $region = $hotel['Hotel']['Region']['Name'];
    $stars = $hotel['Hotel']['Stars'];

    foreach($hotel['Result'] as $result)
    {
         echo $result['Room']['SellingPrice']['Currency'] . ' ' . $result['Room']['SellingPrice']['Amount'];
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

If i use this code , i m getting error - Fatal error: Cannot use string offset as an array. multiple prices hotel are displayed , but if only one room type only one price hotel are not displayed im get error . please resolve this problem
did you try my second solution? you need to place that inside your first loop
I try the second option only . but i get output. In this output multiple price hotels are displayed. Single price hotels are not displayed and getting an error - Fatal error: Cannot use string offset as an array
I tried first one, but i m getting same result . before i get. I need all hotel with all multiple price will be a output i want ????
So you need to use the loop (second option) did you place it inside the first loop? check updated answer please
|
1
foreach($array_data['HotelAvailability'] as $hotel) {
    $id = $hotel['Hotel']['Id'];
    $name = $hotel['Hotel']['Name'];
    $region = $hotel['Hotel']['Region']['Name'];
    $stars = $hotel['Hotel']['Stars'];
    $price = $hotel['Result']['Room']['SellingPrice']['Currency'] . ' ' . $hotel['Result']['Room']['SellingPrice']['Amount'] ;

multiple price fetch

if (is_array($hotel['Result'][0]))
    {



    foreach($hotel['Result'] as $result) 
    {


               $price1 =  $result['Room']['SellingPrice']['Currency'] . ' ' . $result['Room']['SellingPrice']['Amount'];

                echo "$price1 ";



    }

     }

1 Comment

Hope you can accept your own answer. Hope you can up-vote others too:)
0

You are missing a index from as your Result array is multi-dimensional also.

$hotel['Result'][0]['Room']['SellingPrice']['Currency'];
$hotel['Result'][0]['Room']['SellingPrice']['Amount'];

2 Comments

Multiple price available. So i need only loop function. but above loop function was working but i have some error - Fatal error: Cannot use string offset as an array. multiple price hotel are displayed but single price hotels are not displayed . what i do
check it before using. is_array() means you have details such as price..etc !is_array() means is a string/empty so there is no price. Set a default one, ore nothing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.