0

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!

4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by andrewliu
0

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

Edited by diafol: n/a

0

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?

}

Edited by mike_2000_17: Fixed formatting

0

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; 
}
0

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

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.