2011-10-29 169 views
0

你好,我試圖從一個對象數組中打印一個特定的值。我試圖從一個數組名稱$ allPhotos中獲取一個具有「nme」值的對象屬性的值。打印一個數組中的PHP值

這就是我試圖: echo $ allPhotos [0] [「nme」];

這是陣列的樣子:

的var_dump($ allPhotos);

array(2) { 
    [0]=> object(Photo)#1 (10) { 
    ["product"]=> array(5) { 
     ["PKG1"]=> string(4) "6500" 
     ["PKG2"]=> string(4) "9500" 
     ["8x10"]=> string(4) "1500" 
     ["5x7"]=> string(3) "750" 
     ["4x6"]=> string(3) "300" 
    } 
    ["price"]=> NULL ["sku"]=> string(1) "1" 
    ["nme"]=> string(5) "test1" 
    ["dir"]=> string(51) "http://" 
    ["status"]=> string(1) "1" ["gallery"]=> string(16) "Church Directory"  
    ["galleryCover"]=> string(1) "0" 
    ["family"]=> string(0) "" 
    ["familyCover"]=> string(0) "" 
} 

[1]=> object(Photo)#2 (10) { 
    ["product"]=> array(5) { 
    ["PKG1"]=> string(4) "6500" 
    ["PKG2"]=> string(4) "9500" 
    ["8x10"]=> string(4) "1500" 
    ["5x7"]=> string(3) "750" 
    ["4x6"]=> string(3) "300" 
    } 
    ["price"]=> NULL 
    ["sku"]=> string(1) "2" 
    ["nme"]=> string(5) "test2" 
    ["dir"]=> string(51) "http://" 
    ["status"]=> string(1) "1" 
    ["gallery"]=> string(16) "Church Directory" 
    ["galleryCover"]=> string(1) "0" 
    ["family"]=> string(0) "" 
    ["familyCover"]=> string(0) "" 
} 
} 

在此先感謝!

+0

你試過'echo $ allPhotos [0] [「nme」];'? – Dave

+0

是的,我試過了。 – user1019416

回答

4

我認爲echo $allPhotos[0]->nme;應該工作。

+0

我也這麼認爲。沒有這種數組索引的經驗,因爲我還沒有需要它,雖然簡單的Google搜索給了我相同的答案。也可能是這樣的副本:[鏈接](http://stackoverflow.com/questions/5054520/php-how-to-echo-specific-object-data-from-array) – Seralize

+0

是的,就是這樣,非常感謝。只是想知道爲什麼$ allPhotos [0] [「nme」];不行?是因爲它仍然在數組中的一個對象中? – user1019416

+0

該數組是一個對象數組。對象'$ allPhotos [0]'不是一個數組,所以你必須使用' - >'來訪問它的屬性。可以這樣想:'$ n = $ allPhotos [0]; echo $ n-> nme;' – Tim