Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问节点数组_Php_Arrays - Fatal编程技术网

Php 访问节点数组

Php 访问节点数组,php,arrays,Php,Arrays,如何访问阵列节点?例如,字段“cantidad” 结果: Array ( [2] => Array ( [cantidad] => 1 [id_producto] => 2 [precio] => 875 [nombre] => Queso manchego [imagen] => dodgers01.jpg [btn_add_item] => Agregar al carrito ) )

如何访问阵列节点?例如,字段“cantidad”

结果:

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);
?>


如果您要问这个基本问题,我建议您开始阅读PHP手册-对于这个问题,该页面将是一个很好的开始。

显然,您的数组
$carritoal
包含一个索引为
2
的项。此元素的内容本身是一个关联数组

您可以通过其键(索引号)引用该数组,如下所示:

$carritoactual[2]
因此,如果要打印该数组的内容:

print_r( $carritoactual[2] );
这类似于您对
$This->carrito->get_carrito()的结果所做的操作
但它直接使用其键访问元素(
2

现在,如果您想访问
cantidad
,它是
$carritoal[2]
中的元素之一:

print_r( $carritoactual[2]['cantidad'] );
请注意,主数组有一个数字键,而第二个数组有字符串键。在PHP中,可以混合使用数字键和字符串键


当然应该。这是一个数组而不是对象

$Variable[2]['cantidad'];
将为您提供所需的价值