Freedom-n-Democrazy Posted October 8, 2011 Share Posted October 8, 2011 I am currently using a single entity called id for a key. I'm now wanting to use two entities as a key - id and size. I've tried using these two peices of code, but failed: foreach ($_SESSION['cart']['content'] as $key=>$product,$size) { if ($product['id'] == $id && $product['size'] == $size) { $item_found = $key; break; } } // do thing ... and foreach ($_SESSION['cart']['content'] as $key=>$product && $key=>$size) { if ($product['id'] == $id && $product['size'] == $size) { $item_found = $key; break; } } // do thing Am I going the right way about it? Can two entities even be used as a key? Quote Link to comment https://forums.phpfreaks.com/topic/248683-using-two-entities-for-a-key/ Share on other sites More sharing options...
iblood Posted October 8, 2011 Share Posted October 8, 2011 I don't think you're using the function correct. I checked the php manual:http://php.net/manual/en/control-structures.foreach.php, however, I can't find an example like your implementation. Maybe it could help if you would post what you are trying to achieve here, and what your array variable contains. Quote Link to comment https://forums.phpfreaks.com/topic/248683-using-two-entities-for-a-key/#findComment-1277174 Share on other sites More sharing options...
trq Posted October 9, 2011 Share Posted October 9, 2011 Your foreach syntax is wrong. There are many examples in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/248683-using-two-entities-for-a-key/#findComment-1277331 Share on other sites More sharing options...
Drummin Posted October 9, 2011 Share Posted October 9, 2011 I know you've been fighting this in a number of posts. Why not use the product id as the key? Even add the size if this is needed and the value would then be the quantity, i.e. Array("143s" => 3, "122m" =>2). Updating the quantity should be simple as you would know the key represents the product. Just my two cents worth. Quote Link to comment https://forums.phpfreaks.com/topic/248683-using-two-entities-for-a-key/#findComment-1277357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.