2014-02-19 87 views
3

在這個數組函數中我們想獲得酒店的詳細信息。我使用下面的代碼來獲取值。在那段代碼中我沒有得到正確的結果;一些酒店沒有顯示價格。 我也提供了輸出。多維數組獲取php中使用的值每個

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

回答

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

如果我使用此代碼,我m到處錯誤 - 致命錯誤:無法使用字符串作爲陣列偏移。顯示多個價格酒店,但如果只有一個房間類型只有一個價格酒店不顯示即時通訊錯誤。請解決這個問題 – Abiram

+0

你試過我的第二個解決方案嗎?你需要把它放在你的第一個循環中 – Fabio

+0

我只嘗試第二個選項。但我得到輸出。在此輸出中會顯示多個價格酒店。單價酒店沒有顯示,並得到一個錯誤 - 致命錯誤:不能使用字符串偏移量作爲一個數組 – Abiram

0

你缺少一個索引你的結果數組是多維也。

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

多個價格可用。所以我只需要循環功能。但上面的循環功能工作,但我有一些錯誤 - 致命錯誤:不能使用字符串偏移作爲數組。顯示多個價格酒店,但不顯示單個價格酒店。我在做什麼 – Abiram

+0

使用前檢查它。 is_array()表示您有詳細信息,如價格..等等 !is_array()表示是一個字符串/空,所以沒有價格。設置默認的一個,什麼都不要 –

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



    } 

    } 
+0

希望你能接受你自己的答案。希望你也能投票贊成其他人:) –