Php 无法调用多维数组
我试图用Ajax和多维会话数组创建一个购物车系统。数组存储产品id和此id的数量。我试图显示其中一个存储id的数量,但未返回正确的值 这是我的php代码Php 无法调用多维数组,php,arrays,session,multidimensional-array,Php,Arrays,Session,Multidimensional Array,我试图用Ajax和多维会话数组创建一个购物车系统。数组存储产品id和此id的数量。我试图显示其中一个存储id的数量,但未返回正确的值 这是我的php代码 public function create() { session_start(); $product_id = $_GET['data']; // Check if the cart has been created if(isset($_SESSION['cart'
public function create()
{
session_start();
$product_id = $_GET['data'];
// Check if the cart has been created
if(isset($_SESSION['cart']) AND $_SESSION['cart'] != NULL)
{
// One or more items are have already been placed in the cart
}else{
// The cart is empty
$_SESSION['cart'] = array();
$_SESSION['cart'][$product_id] = array('quantity' => 3);
}
// Return the quantity of a stored product id
return $_SESSION['cart'][$product_id]['quantity'];
}
如您所见,应该返回数字3。我得到的是值1。可能已经设置好了?设置了1吗?
print\r($\u SESSION['cart'])
output是什么?我不能使用print\r,因为我的响应是用ajax返回的:/运行print\r并在firebug或任何其他非常有用的开发工具中检查输出。return$\u SESSION['cart'][$product\u id][0]['quantity'];