Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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
PHP5中的多维数组_Php_Multidimensional Array_Php 5.5 - Fatal编程技术网

PHP5中的多维数组

PHP5中的多维数组,php,multidimensional-array,php-5.5,Php,Multidimensional Array,Php 5.5,我有一个简单的问题,但我无法做到这一点 我的系统中有一个多维数组: array (size=5) 'id_cli' => string '13' (length=2) 'login_cli' => string 'userlogin' (length=10) 'senha_cli' => string 'userpass' (length=3) 'cli_nome' => string 'username' (length=16) 'cart' =&g

我有一个简单的问题,但我无法做到这一点

我的系统中有一个多维数组:

array (size=5)
  'id_cli' => string '13' (length=2)
  'login_cli' => string 'userlogin' (length=10)
  'senha_cli' => string 'userpass' (length=3)
  'cli_nome' => string 'username' (length=16)
  'cart' => 
    array (size=3)
      'id' => int 48
      'tamanho' => string 'G' (length=1)
      'qtde' => int 1
此数组保存在我的
$\u会话中
。购物车密钥用于保存我的购物车产品。在上面的例子中,我有一个产品

我需要在会话阵列购物车中保存新产品,但我不能这样做。我尝试插入的每个新产品都会在第一个产品上保存,而不是插入

我正在这样做:

  • 声明我的购物车阵列:

    if(!isset($_SESSION['cart']))
    {
        $_SESSION['cart'] = array();
    }
    
  • 在我的会话中插入新产品:

    $_SESSION['cart'] = array("id" => $id_produto, "tamanho" => $tamanho_produto, "qtde" => 1);
    
但它不起作用。如何在我的
$\u会话['cart']
中插入新数组

我需要做什么才能删除此
$\u会话['cart']
数组中的特定产品

谢谢,
Marcelo.

您需要以数组的形式插入新产品。试着这样做:

$_SESSION['cart'][] = array("id" => $id_produto, "tamanho" => $tamanho_produto, "qtde" => 1);

您可以使用
array\u push($\u SESSION['cart'],$newElement)
将数组元素插入现有数组

另一种方法是
$\u SESSION['cart'][]=$newElement

要删除特定元素,可以使用以下逻辑

 $arrayKey=array_search($arrayKeyName,$_SESSION['cart']);
 if($arrayKey!==false) unset($_SESSION['cart'][$arrayKey]);
另一种方法

foreach($_SESSION['cart'] as $k => $v) {
  if($v == $arrayKeyName)
    unset($_SESSION['cart'][$k]);
}