Abiram
Abiram

Reputation: 59

Multidimensional array to get value in php using for each

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"; 
   }

Upvotes: 3

Views: 192

Answers (3)

Abiram
Abiram

Reputation: 59

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



    }

     }

Upvotes: 1

Fabio
Fabio

Reputation: 23510

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'];
    }
}

Upvotes: 1

Pian0_M4n
Pian0_M4n

Reputation: 2558

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'];

Upvotes: 0

Related Questions