2014-04-06 122 views
0

這是我的多維關聯的JavaScript數組,我在PHP收到它:我如何訪問PHP中的多維關聯JS數組值?

array (size=2) 
0 => 
    object(stdClass)[1] 
     public 'count' => string '30' (length=2) 
     public 'size' => string '1' (length=0) 
     public 'name' => string 'aa' (length=0) 
     public 'pid' => string '910' (length=0) 
     public 'price' => string '300' (length=0) 
    1 => 
    object(stdClass)[2] 
     public 'count' => string '30' (length=2) 
     public 'size' => string '2' (length=0) 
     public 'name' => string 'bb' (length=0) 
     public 'pid' => string '920' (length=0) 
     public 'price' => string '400' (length=0) 

如何訪問多維關聯數組JS價值PHP?

+0

問題的答案,但我有一個額外的註釋 - 我假設你用'json_decode()'在你的代碼,並得到了上述結果?如果是這樣,值得注意的是'json_decode()'默認爲JS對象文字創建'stdClass'對象實例。但是,您可以通過將'true'作爲第二個參數傳遞給'$ assoc'參數來強制執行PHP關聯數組;例如:'json_decode($ json,true);'。示例:https://eval.in/132718 - 更多關於'json_decode'的信息:http://www.php.net/manual/en/function.json-decode.php –

回答

2

你必須把它作爲一個對象,例如

foreach($array as $obj) echo $obj->count; 
+0

它的工作,謝謝。 – Unique

+0

如何分別訪問'object(stdClass)[1]'或'object(stdClass)[2]'值? – Unique

+0

@Unique'$ array [0] - > property'或'$ array [1] - > property' – Sam