scottybwoy Posted August 18, 2008 Share Posted August 18, 2008 Is their a problem using $_SESSION variables in array_key_exists? I use it, it works but brings up a notice. Is there any reason why I shouldn't use it, if the session var is actually an array? Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/ Share on other sites More sharing options...
kenrbnsn Posted August 18, 2008 Share Posted August 18, 2008 Please show us the code you're using. Ken Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619174 Share on other sites More sharing options...
scottybwoy Posted August 18, 2008 Author Share Posted August 18, 2008 Sorry, thought it was a simple question //If product already exists, update the row. if (array_key_exists($modelId, $_SESSION['item_buy'])) { $price = number_format($price, 2, '.', ''); $session_qty = $_SESSION['item_buy'][$modelId][1] + $qty; $prod_net = number_format($qty * $price, 2, '.', ''); $prod_vat = number_format($prod_net * VAT, 2, '.', ''); $prod_gross = number_format($prod_net + $prod_vat, 2, '.', ''); $_SESSION['item_buy'][$modelId] = array($price, $session_qty, $prod_net, $prod_vat, $prod_gross); } else { $_SESSION['item_buy'][$modelId] = array($price, $qty, $prod_net, $prod_vat, $prod_gross); } Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619178 Share on other sites More sharing options...
kenrbnsn Posted August 18, 2008 Share Posted August 18, 2008 What is the "notice" you're getting? Ken Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619197 Share on other sites More sharing options...
scottybwoy Posted August 18, 2008 Author Share Posted August 18, 2008 The notice is that the second argument must be an array, but it is not possible for $_SESSION['item_buy'] to be anything but an array. If I print_r it, it also shows as an array. [edit] I am not that bothered, but just wanted to know if I was using it correctly. Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619249 Share on other sites More sharing options...
PFMaBiSmAd Posted August 18, 2008 Share Posted August 18, 2008 The posted code works for me when $_SESSION['item_buy'] exists and is an array for both a true and false array_key_exists() result. At the time your code is being executed, $_SESSION['item_buy'] either does not exist at all or is not an array. At what point are you doing the print_r()? If $_SESSION['item_buy'] does not exist, you get a notice message about the item_buy index and a warning message about the second argument should be either an array or an object in ... What is the exact error message? Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619257 Share on other sites More sharing options...
scottybwoy Posted August 18, 2008 Author Share Posted August 18, 2008 Ok, no you are correct. This function is also used to determine whether $_SESSION['item_buy'] exists. So it would be better to use : //If product already exists, update the row. if (isset($_SESSION['item_buy']) && array_key_exists($modelId, $_SESSION['item_buy'])) { $price = number_format($price, 2, '.', ''); $session_qty = $_SESSION['item_buy'][$modelId][1] + $qty; $prod_net = number_format($qty * $price, 2, '.', ''); $prod_vat = number_format($prod_net * VAT, 2, '.', ''); $prod_gross = number_format($prod_net + $prod_vat, 2, '.', ''); $_SESSION['item_buy'][$modelId] = array($price, $session_qty, $prod_net, $prod_vat, $prod_gross); } else { $_SESSION['item_buy'][$modelId] = array($price, $qty, $prod_net, $prod_vat, $prod_gross); } Correct? Is there a better way to write this function? Link to comment https://forums.phpfreaks.com/topic/120190-_sessionvariables-and-array_key_exists/#findComment-619270 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.