Jump to content

Updating an array key value


Phaelon

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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 by Phaelon
Link to comment
Share on other sites

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...
	}
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.