Phaelon Posted May 6, 2014 Share Posted May 6, 2014 Hi all,I have made the following variable with arrays: $_SESSION['cart']['content'] = array( array(number => 1, sizes => 1) , array(number => 2, sizes => 1) ); Can someone please tell me how I can update the key 'sizes' where 'number = 2' without it touching anything else? Quote Link to comment Share on other sites More sharing options...
Phaelon Posted May 7, 2014 Author Share Posted May 7, 2014 To further explain on what I am trying to do, in MySQL I would do the equivilent of something along the lines of: UPDATE content SET sizes = 2 WHERE number = 2; I'm basically wanting to do the same, but for an array in a PHP session. Perhaps I have engineered my session and array wrong for a cart system? Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 7, 2014 Share Posted May 7, 2014 (edited) foreach ($_SESSION['cart']['content'] as $key=>$value){ if ($value==2){ $_SESSION['cart']['content'][$key]=2; } } I'd try something like this Edited May 7, 2014 by davidannis Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted May 7, 2014 Share Posted May 7, 2014 did you read the latest reply in your previous thread? if the 'number' element you are showing represents the item number and you are going to need to reference the entries in the cart by their item number, use the item number as an array index, not as a separate element in the stored array. once you do that, your code would be - $_SESSION['cart']['content'][2]['sizes'] = 2; Quote Link to comment Share on other sites More sharing options...
Phaelon Posted May 7, 2014 Author Share Posted May 7, 2014 (edited) Thanks davidannis, I tried your suggestion, but it didn't work. $_SESSION['cart']['content'] = array( array(number => 1, sizes => 1) , array(number => 2, sizes => 1) ); foreach ($_SESSION['cart']['content'] as $content) { echo 'PRODUCT NUMBER: '.$content['number'].'<BR>'; if (isset($content['sizes'])) {echo 'SIZE S: '.$content['sizes'].'<BR>';} if (isset($content['sizem'])) {echo 'SIZE M: '.$content['sizem'].'<BR>';} echo '<BR>'; } foreach ($_SESSION['cart']['content'] as $key=>$value){ if ($value==2){ $_SESSION['cart']['content'][$key]=2; } } foreach ($_SESSION['cart']['content'] as $content) { echo 'PRODUCT NUMBER: '.$content['number'].'<BR>'; if (isset($content['sizes'])) {echo 'SIZE S: '.$content['sizes'].'<BR>';} if (isset($content['sizem'])) {echo 'SIZE M: '.$content['sizem'].'<BR>';} echo '<BR>'; } Thank you for the information mac_gyver. I just tried to do that, but it didn't work: $_SESSION['cart']['content'] = array( array(number => 1, sizes => 1) , array(number => 2, sizes => 1) ); foreach ($_SESSION['cart']['content'] as $content) { echo 'PRODUCT NUMBER: '.$content['number'].'<BR>'; if (isset($content['sizes'])) {echo 'SIZE S: '.$content['sizes'].'<BR>';} if (isset($content['sizem'])) {echo 'SIZE M: '.$content['sizem'].'<BR>';} echo '<BR>'; } $_SESSION['cart']['content'][2]['sizes'] = 2; foreach ($_SESSION['cart']['content'] as $content) { echo 'PRODUCT NUMBER: '.$content['number'].'<BR>'; if (isset($content['sizes'])) {echo 'SIZE S: '.$content['sizes'].'<BR>';} if (isset($content['sizem'])) {echo 'SIZE M: '.$content['sizem'].'<BR>';} echo '<BR>'; } Edited May 7, 2014 by Phaelon Quote Link to comment Share on other sites More sharing options...
adam_bray Posted May 7, 2014 Share Posted May 7, 2014 Thank you for the information mac_gyver. I just tried to do that, but it didn't work: It didn't work because you didn't do what he suggested, you jumped to the end without changing anything. Perhaps I have engineered my session and array wrong for a cart system? Yes^ You need to take the number variable out of the final array and use it as an array key. Here's an example - <?php // Array to store the shopping cart $cart = array(); // Products that are available // Product_id => array of product info // Could be from database instead $products = array( // Product 1 1 => array( 'size' => 'L', 'available' => 3, 'price' => '4.00', ), // Product 2 2 => array( 'size' => 'XL', 'available' => 8, 'price' => '4.00', ), // Product 3 3 => array( 'size' => 'S', 'available' => 1, 'price' => '3.50', ), ); // If a product id has been set if( isset( $_GET['pid'] ) ) { // Add the product to your cart variable $cart[$_GET['pid']][] = $products[$_GET['pid']]; // Tell the user what they've added print 'You have added a shirt in '.$cart[$_GET['pid']]['size'].' for $'.$cart[$_GET['pid']]['price'].' to your cart.'; // Store in session // Check it isn't already in the cart // Etc... } ?> Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 7, 2014 Share Posted May 7, 2014 Oops, the line: if ($value==2){ should have been if ($value['number']==2){ I haven't seen the previous thread but mac_gyver is right that you should take out number if it is just a key and use it as such. I assumed number was a product number, isbn number, or something like that. Quote Link to comment 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.