2011-11-30 227 views
0

我正在使用多維數組; 我使用arsort函數來獲取已添加最新的數組。當我想使用這個數組我做的這一切工作正常PHP多維數組鍵值

arsort($this->shoppingBag); 
$this->productToShow = key($this->shoppingBag); 

$prodName = key($this->shoppingBag[$this->productToShow]); 

這給了我,我需要正確的名稱正確的產品。當我做

$count = $this->shoppingBag[$this->productToShow[$prodName]]; 

它給了我一個「未定義的索引」的錯誤。

當我回聲鑰匙作爲字符串我從陣列得到正確的值數組..

這是爲什麼,我怎麼能得到與該鍵的值?

編輯:

array(4) 
{ 
    [38] => array(1) 
    { 
     ["SAMSUNG LE32D450"] => int(3) 
    } 
    [32] => array(1) 
    { 
     ["Inspiron 15R"] => int(1) 
    } 
    [29] => array(1) 
    { 
     ["XPS 15"] => int(25) 
    } 
    [37] => array(1) 
    { 
     ["Logitech M185 Black"] => int(10) 
    } 
} 
+3

您能否包含多維數組數據的示例? –

+0

是的,現在包含數組。 – kendepelchin

+1

檢查我最近的編輯。我很確定這應該工作。 –

回答

1

這是一樣簡單$this->productToShow僅僅是一個關鍵變量,而不是一個數組。所以對該變量的索引的調用是未定義的。那麼你不會找到答案是$count = $this->shoppingBag[$this->productToShow][$prodName];

+0

$ this-> productToShow = key($ this-> shoppingBag); – kendepelchin

+0

工作。謝謝 的確看起來很簡單,我想我今天正在爲這些陣列做很多事情。謝謝您的幫助。 – kendepelchin

+1

@turncoat然後不要忘記接受答案。 –

0

嘗試:

$count = $this->shoppingBag[$this->productToShow]; 
+0

這給了我數組 – kendepelchin

+0

是數組中的變量之一嗎?或者計數數組中的元素數量? –

0

$this->productToShow不是數組。所以看到,因爲$prodName$this->shoppingBag特定項目的關鍵,

我假設你想$ count來返回最後一個關鍵,因爲如果我們忽略了正確應用[$prodName]$this->productToShow鍵,$數將返回值購物袋中的最後一把鑰匙。