多维数组,以获取每个PHP使用的值

时间:2014-02-19 11:27:33

标签: php arrays multidimensional-array

在这个数组函数中,我们想获得酒店的详细信息。我使用以下代码来获取值。在那段代码中,我没有得到正确的结果;一些酒店没有显示价格。 我也提供了输出。

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
                                )

                        )

                )

输出:

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

我的代码:

    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 个答案:

答案 0 :(得分:1)

正如您所见,Result有多个子阵列,如果您需要有与第一个条目相关的详细信息,您可以使用其索引0

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

您可以使用您需要的任何结果数组更改索引[0],否则您可以循环抛出Result子数组并检索有关它们的所有详细信息

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

答案 1 :(得分: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'] ;

多次提价

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



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


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

                echo "$price1 ";



    }

     }

答案 2 :(得分:0)

您缺少索引,因为您的Result数组也是多维的。

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