2013-10-23 124 views
-1

我如何訪問數組節點?例如,字段「cantidad」?訪問節點數組PHP

結果:

Array ( 
    [2] => Array (
    [cantidad] => 1 
    [id_producto] => 2 
    [precio] => 875 
    [nombre] => Queso manchego 
    [imagen] => dodgers01.jpg 
    [btn_add_item] => Agregar al carrito 
) 
) 

我的代碼是:

<?php 
$carritoactual = $this->carrito->get_carrito(); 
print_r($carritoactual); 
?> 

回答

1

如果你問的問題此基礎上,我建議你開始在PHP手冊讀了 - 對於這個問題,Arrays頁面將是一個很好的開始。

0

顯然您的數組$carritoactual包含索引爲2的一個項目。這個元素的內容本身就是一個關聯數組。

你會通過它的鍵(索引號)這樣的指的是陣列:

$carritoactual[2] 

所以,如果你想打印數組的內容:

print_r($carritoactual[2]); 

這類似於與你對$this->carrito->get_carrito();的結果做了什麼,但它直接使用它的鍵(2)訪問該元素。

現在如果你想訪問cantidad,這裏面$carritoactual[2]的要素之一:

print_r($carritoactual[2]['cantidad']); 

注意,主陣列有一個數字鍵,但第二陣列具有字符串鍵。在PHP中,您可以混合使用數字鍵和字符串鍵。

當然你應該read the manual

0

這是一個數組,而不是一個對象

$Variable[2]['cantidad']; 

會得到你想要的值。