Hello all!

I have set up a multidimensional array, and I was wondering how would I update a value within the array?

Like how if you update a value in mysql, u use mysql_query("UPDATE....

Simply the same thing I want to achieve in an array. I just wanted to update a value in an array.


Any suggestions? Thanks!

Member Avatar for diafol

Perhaps you'd like to share your code? How deep do you do? Which keys do you use, if at all?

I set my array in a session

    $_SESSION["cart_array"]=array ( 
                                  0 => array (
                                             "item_id" => $pid,
                                             "quantity" => 1, 
                                             "item_size" => $size, 
                                             "item_color" => $color
                                             )
                                   ); 

and it goes through a loop so it will increment itself. so it will have a 0, 1, 2, 3, etc... But for simplicity. I want to be able to update this array and the values I want to update are color, quantity, and size.

I'm having a form that i can use to update

<form name='updateForm' action='' method='post'>

<input size.../>
<input color.../>
<input quantity.../>
<input type='hidden' value='0' name='changeOptions'/> 
<form class='updateForm' id='updateForm' name='updateForm' method='post' action='cart.php'>

Now Im thinking that I can do something like this

if(isset($_POST['changeOptions'])){
// this is where I can update my session array?

}

So what is the problem? Catch up the post value and change the array

if(isset($_POST['changeOptions'])){
//grab your POST value
$someValue = $_POST["SomeFormValue"];
$myArrayName = $_SESSION["cart_array"];
$myArrayName[0]["item_id"] = $someValue; 
}

thanks just trying to figure out how to write it up. got it thanks

thanks just trying to figure out how to write it up. got it thanks

All the best!

So I tried doing it,

with this code

if(isset($_POST['updateOptions']) && isset($_POST['updateColor']) && isset($_POST['updateSize'])){
	$myArray = $_SESSION["cart_array"];
	$newValues = $_POST['updateOptions'];
	$updateColor = $_POST['updateColor'];
	$updateSize = $_POST['updateSize'];
	$myArray[$i]['item_size'] = $updateSize;
	$myArray[$i]['item_color'] = $updateColor;
}

its not showing the results i wanted.

This should work, correct?

$myArray[$i]['item_size'] = $updateSize;
$myArray[$i]['item_color'] = $updateColor;

what is $i ?

sorry i didnt clarify. its the number of which the array is set to. my $i is set through a while loop.

so it would be 0,1,2,3,4.. etc

$_SESSION["cart_array"]=array ( 
						 0 => array (
							"item_id" => $pid,
							"quantity" => 1, 
							"item_size" => $size, 
							"item_color" => $color
							)
			);

But in this case its 0. and then later on I will have another array which would be 1. So the $i is set to which array should be updated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.