2011-11-16 49 views
1

我有一個json解碼數組像$ json = json_decode($ curl_result);如何使用foreach循環獲取這個數組,我想盡可能地減少數組循環。你能寫出正確的代碼,我可以訪問這些值。獲取JSON解碼數組使用PHP foreach循環

陣列看上去就像下面

Array 
    (
    [SITEResponse] => Array 
    (
     [@version] => 1.0 
     [SoftwareProducts] => Array 
     (
      [@numFound] => 408 
      [@numReturned] => 10 
      [@start] => 0 
      [SoftwareProduct] => Array 
      (
       [0] => Array 
        (
        [Summary] => Array 
        (
         [$] => summery of software. 
        ) 
        [Requirements] => Array 
        (
        ) 

        [ContentIds] => Array 
        (
        ) 

        [CleverBridgeUrl] => Array 
        (
        ) 

        [BuyNowUrl] => Array 
        (
         [$] => http://www.abc.com 
         [@type] => dl_buy_pub 
        ) 

        [BetaRelease] => Array 
        (
         [$] => false 
        ) 

        [LinkURL] => Array 
        (
        [$] => http://www.abc.com 
        ) 
       )  
      ) 
     ) 
    ) 
) 

現在我想這個數組的每一個值,但如何???請幫助 謝謝。

+0

使用foreach循環.. – insomiac

+0

但不工作 –

+0

創建一個功能,讓你在一個動態的方式輸出。 例如:的foreach($ JSON作爲$密鑰=> $ val)的{ 如果(is_array($ VAL)){ CALL,其掃描數組的函數.. } } 否則可以通過指定做靜態數組名稱: $ output ['SITEResponse']將數組 $ output ['SITEResponse'] ['version']將爲1.0 類似地,您可以執行此其他值。 – insomiac

回答

1
$version = $json['SITEResponse']['@version']; 
$numFound = $json['SITEResponse']['SoftwareProducts']['@numFound']; 
... 
... 

foreach($json['SITEResponse']['SoftwareProducts']['SoftwareProduct'] as $key=>$product){ 
$Summary= $product['Summary']['$']; 
$BuyNowUrl = $product['BuyNowUrl']['$']; 
... 
... 
... 
}