2013-03-12 81 views
1
var_dump($products); 

輸出檢索值:array(7) { [0]=> array(5) { ["Product"]=> array(40) { ["id"]=> string(3) "726" ["wbb"]=> string(1) "1" ["abb"]=> string(1) "0" }轉儲陣列,如何從陣列

但是當我做var_dump($products['Product']['wbb']);它給了我NULL

我將如何使用[「WBB」]的價值

if(!empty($product)) : 

如果我轉儲$產品,它會給我不同的產品,每個產品的細節。
一個產品的簡單輸出,當我傾倒$產品

array(7) { [0]=> array(5) { ["Product"]=> array(40) { ["id"]=> string(3) "726" ["wbb"]=> string(1) "1" ["abb"]=> string(1) "0" } 

{ [1]=> array(5) { ["Product"]=> array(40) { ["id"]=> string(3) "727" ["wbb"]=> string(1) "0" ["abb"]=> string(1) "1" } 

同樣地,我有10多個產品,其中有些是WBB = 1,其中有些是ABB = 1。我想根據提出的標誌添加工具提示。但是當我這樣做時 $ i ++;

if(!empty($product[$i]['Product']['wbb'])){ echo code here.....} 

因爲它迭代,所以它不會根據確切的產品給我。我該怎麼做呢?

+1

$ products [0] ['Product'] ['wbb'] – 2013-03-12 15:56:14

+1

'$ product'變量來自哪裏?你對你的'$產品'進行foreach嗎?請分享*所有*相關代碼,否則我們無法幫助您! – Oldskool 2013-03-12 16:41:09

回答

2

看起來你應該使用var_dump($products[0]['Product']['wbb']);

2

如果你仔細觀察,你會發現,$products數組是一個多維數組,持有每一個數據行(從0開始)的數字鍵。任何額外的行將得到1,2等等。爲了得到第一個(顯然只)行提到wbb值,使用

var_dump($products[0]['Product']['wbb']); 

相反。

+0

我也試過這個,但同樣的NULL值 – Akaash 2013-03-12 16:00:02

+1

@AbrarKiyani在這種情況下,你沒有向我們展示你的所有代碼。如果我在粘貼數組時重新創建數組,並使用上面的變量,則此代碼示例輸出'WBB爲1',正如所期望的那樣:https://gist.github.com/oldskool/5144154 – Oldskool 2013-03-12 16:06:45

2

$products是多個產品的數組。您應該使用:

var_dump($products[0]['Product']['wbb']); 
+1

然後你會調用' $ products'如果[0]鍵不在那裏?無論如何,它仍然是一個數組,這個只是多維的,並且在每個結果行上進行數字索引。 – Oldskool 2013-03-12 15:58:11

+0

你是對的! – 2013-03-12 16:03:31