php - Not able to retrieve cookie data from a multidimensional array -
this question has answer here:
- php - setcookie(); not working 4 answers
i'm new cookies here how i'm setting , retrieving data
if(!isset($_cookie['cart'])){ $_cookie['cart'] = array(); } setcookie("cart[$stk_id]['name']", $name, time()+24*60*60, "/"); setcookie("cart[$stk_id]['quantity']", $qty, time()+24*60*60, "/"); setcookie("cart[$stk_id]['vendor']", $vendor, time()+24*60*60, "/"); foreach ($_cookie['cart'] $stk_id => $product){ $qty = $product['quantity']; $pro_name = $product['name']; }
but i'm getting error notice: undefined index: quantity , name. problem?
cookies array store commonly used once variable ,you set cookies name in array,but create array first , after set cookie name more convenient.here can set json_encode
, json_decode
$data = array($stk_id => array( "name" => $name, "quantity" => $qty, "vendor" => $vendor ) ); setcookie("cart", json_encode($data), time()+24*60*60, "/"); $cookie = json_decode($_cookie['cart'],true);//for array output foreach ($cookie $stk_id => $product){ $qty = $product['quantity']; $pro_name = $product['name']; }
Comments
Post a Comment