Problem with PHP Array

0

I do not have much experience in PHP. I had a foreach taking out the information from this Array:

foreach($_SESSION["cart_item"] as $k => $v)

Now I have added this to the Array:

foreach($_SESSION[$clie]["cart_item"] as $k => $v) 

And I do not know how to change the $ k and $ v keys so that it keeps working. Can someone help me?

    
asked by Cricket 23.01.2017 в 18:34
source

1 answer

1

I imagine you have structured this way:

$ clie is an array of client properties and cart_item is the customer's cart

Within cart_item you have the items ($ k) and the amounts ($ v) and what you want is to eliminate or change articles and their respective quantities ...

If so, you need to change the values by reference or create a new array ... example:

$cart = $_SESSION[$clie]['cart_item'];

foreach($cart as $item => $valor){
   // Cambiar valor
   $cart[$item] = 'Nuevo valor';

   // O si deseas eliminar un item
   unset($cart[$item]); 

   // Tambien por referencia 
   unset(&$item);
}

The amperstamp makes the reference to the variable, give it a studied parameter by reference and value.

There are functions like array_key_exists () or array_search () , etc. ... to manipulate the ARRAY, here the documentation of the: PHP array functions

What if you need to explain more detailed, because we do not know what changes you want to make.

    
answered by 24.01.2017 в 03:23