如何使用php获取foreach循环中的多维数组值

时间:2022-08-27 18:04:39

This my code ..... I need output of hotel name, id, selling price etc array value will be loop because I have get result in bulk manner .

这个我的代码.....我需要输出酒店名称,id,销售价格等数组值将循环,因为我得到批量方式的结果。

Array
(
    [TestMode] => true
    [HotelAvailability] => Array
        (
            [0] => Array
                (
                    [Hotel] => Array
                        (
                            [Id] => 3027405
                            [Name] => Royal Plaza
                            [Region] => Array
                                (
                                    [Id] => 16849
                                    [Name] => Chennai (Madras)
                                )

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

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

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

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

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

                                                            [Surname] => Array
                                                                (
                                                                )

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

                                                        )

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

                                                            [Surname] => Array
                                                                (
                                                                )

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

                                                        )

                                                )

                                        )

                                    [Confirmation] => allocation
                                )

                        )

                )

            [1] => Array
                (
                    [Hotel] => Array
                        (
                            [Id] => 2118726
                            [Name] => Days Inn Deccan Plaza
                            [Region] => Array
                                (
                                    [Id] => 16849
                                    [Name] => Chennai (Madras)
                                )

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

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

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

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

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

                                                            [Surname] => Array
                                                                (
                                                                )

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

                                                        )

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

                                                            [Surname] => Array
                                                                (
                                                                )

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

                                                        )

                                                )

                                        )

                                    [Confirmation] => allocation
                                )

                        )

                )

            [2] => Array
                (
                    [Hotel] => Array
                        (
                            [Id] => 723729
                            [Name] => Green Park
                            [Region] => Array
                                (
                                    [Id] => 16849
                                    [Name] => Chennai (Madras)
                                )

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

                    [Result] => Array
                        (
                            [0] => Array
                                (
                                    [QuoteId] => 12249136-33
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 1004527
                                                    [Text] => Double Or Twin Deluxe
                                                )

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

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

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

                                                                    [Surname] => Array
                                                                        (
                                                                        )

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

                                                                )

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

                                                                    [Surname] => Array
                                                                        (
                                                                        )

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

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                            [1] => Array
                                (
                                    [QuoteId] => 12249136-34
                                    [Room] => Array
                                        (
                                            [RoomType] => Array
                                                (
                                                    [Code] => 2062742
                                                    [Text] => Double or Twin BUSINESS-CLUB
                                                )

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

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

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

                                                                    [Surname] => Array
                                                                        (
                                                                        )

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

                                                                )

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

                                                                    [Surname] => Array
                                                                        (
                                                                        )

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

                                                                )

                                                        )

                                                )

                                            [Confirmation] => allocation
                                        )

                                )

                        )

                )

2 个解决方案

#1


0  

foreach($data['HotelAvailability'] AS $record) {
     echo 'Id: '; echo $record['Hotel']['Id'];
     echo '<br />';
     echo 'Name: '; echo $record['Hotel']['Name'];
     echo '-------------'; echo '<br />';
}

#2


1  

If I understood your question correctly, you would like to extract the Hotel Id, Name, and SellingPrice keys from this array, in a loop.

如果我正确理解了您的问题,您希望在循环中从此数组中提取Hotel Id,Name和SellingPrice键。

If we assume the above array is stored in a variable called $HotelData, and that the array structure is consistent with the above output in your question, then the following code should allow you to do what you want...

如果我们假设上面的数组存储在一个名为$ HotelData的变量中,并且数组结构与你问题中的上述输出一致,那么下面的代码应该允许你做你想要的...

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

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

/* This should output something like
*  
*  3027405 - Royal Plaza: INR 1824.26
*  2118726 - Days Inn Deccan Plaza - INR 3192.90
*  ...
*/

#1


0  

foreach($data['HotelAvailability'] AS $record) {
     echo 'Id: '; echo $record['Hotel']['Id'];
     echo '<br />';
     echo 'Name: '; echo $record['Hotel']['Name'];
     echo '-------------'; echo '<br />';
}

#2


1  

If I understood your question correctly, you would like to extract the Hotel Id, Name, and SellingPrice keys from this array, in a loop.

如果我正确理解了您的问题,您希望在循环中从此数组中提取Hotel Id,Name和SellingPrice键。

If we assume the above array is stored in a variable called $HotelData, and that the array structure is consistent with the above output in your question, then the following code should allow you to do what you want...

如果我们假设上面的数组存储在一个名为$ HotelData的变量中,并且数组结构与你问题中的上述输出一致,那么下面的代码应该允许你做你想要的...

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

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

/* This should output something like
*  
*  3027405 - Royal Plaza: INR 1824.26
*  2118726 - Days Inn Deccan Plaza - INR 3192.90
*  ...
*/