Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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

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_Session_Multidimensional Array - Fatal编程技术网

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'

我试图用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']) 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'];