2011-07-06 78 views
-1

你好人有問題,數組幫助

我有一個由webservice返回的數組。

Array 
(
[GetProductResult] => Array 
    (
     [schema] => Array 
      (
       [element] => Array 
        (
         [complexType] => Array 
          (
           [choice] => Array 
            (
             [element] => Array 
              (
               [complexType] => Array 
                (
                 [sequence] => Array 
                  (
                   [element] => Array 
                    (
                     [0] => Array 
                      (
                       [!name] => codigo 
                       [!minOccurs] => 0 
                      ) 

                     [1] => Array 
                      (
                       [!name] => nome 
                       [!minOccurs] => 0 
                      ) 

                     [2] => Array 
                      (
                       [!name] => imagem 
                       [!minOccurs] => 0 
                      ) 

                     [3] => Array 
                      (
                       [!name] => stock 
                       [!minOccurs] => 0 
                      ) 

                    ) 

                  ) 

                ) 

               [!name] => produto 
              ) 

             [!minOccurs] => 0 
             [!maxOccurs] => unbounded 
            ) 

          ) 

         [!name] => produtos 
         [!msdata:IsDataSet] => true 
         [!msdata:UseCurrentLocale] => true 
        ) 

       [!id] => produtos 
      ) 

     [diffgram] => Array 
      (
       [produtos] => Array 
        (
         [produto] => Array 
          (
           [codigo] => 13251 
           [nome] => Nova Development - Print Explosion Deluxe Mac 
           [imagem] => http://www.novadevelopment.com/images/3DBox_w250_tcm23-129399.jpg 
           [stock] => 0 
           [!diffgr:id] => produto1 
           [!msdata:rowOrder] => 0 
          ) 

        ) 

      ) 

    ) 

我從一個Web服務使用id_produto從我的數據庫請求,但有時我得到這個錯誤:

Cannot use string offset as an array in php

和陣列的結果是:

Array 
(
[GetProductResult] => Array 
    (
     [schema] => Array 
      (
       [element] => Array 
        (
         [complexType] => Array 
          (
           [choice] => Array 
            (
             [element] => Array 
              (
               [complexType] => Array 
                (
                 [sequence] => Array 
                  (
                   [element] => Array 
                    (
                     [0] => Array 
                      (
                       [!name] => codigo 
                       [!minOccurs] => 0 
                      ) 

                     [1] => Array 
                      (
                       [!name] => nome 
                       [!minOccurs] => 0 
                      ) 

                     [2] => Array 
                      (
                       [!name] => imagem 
                       [!minOccurs] => 0 
                      ) 

                     [3] => Array 
                      (
                       [!name] => stock 
                       [!minOccurs] => 0 
                      ) 

                    ) 

                  ) 

                ) 

               [!name] => produto 
              ) 

             [!minOccurs] => 0 
             [!maxOccurs] => unbounded 
            ) 

          ) 

         [!name] => produtos 
         [!msdata:IsDataSet] => true 
         [!msdata:UseCurrentLocale] => true 
        ) 

       [!id] => produtos 
      ) 

     **[diffgram] =>** 
    ) 

現在我如何避免這個錯誤?即時嘗試訪問陣列與此:

$stock = $result['GetProductResult']['diffgram']['produtos']['produto']['stock']; 

有人可以幫助一個如果要通過錯誤?

Cumps

+1

我不知道如何解決你的問題。我只想說 - 你可憐的靈魂。像這樣的陣列應該被禁止。也許我們可以修改日內瓦公約來把它們定義爲酷刑? –

+0

老實說,web服務的部分看起來是錯誤的編碼。我無法想象一個數組鍵「!msdata:IsDataSet」...看起來像一個函數調用。 –

+0

當你得到錯誤時,第二個數組是一個「$ result」的var_dump? – ianbarker

回答

0

這錯誤通常意味着在您試圖訪問一個數組的項目實際上是一個字符串。確保數組中的所有嵌套元素實際上都是數組,而不是字符串。

+0

如何知道$ result ['GetProductResult'] ['diffgram']是數組還是字符串? –

+0

['is_array'](http://php.net/manual/function.is-array.php)似乎正在做你所需要的。 – Lumbendil

+0

if(is_array($ result ['GetProductResult'] ['diffgram'])){...} –