-1

I am having trouble assigning a variable to an array within the $_SESSION array.

It looks like it is assigning, but when I do a print_r at the end of the program the $_SESSION variable appears unchanged.

Here is the code.

<?php
session_start();

print_r($_SESSION[cart_array]);

$NewGroupName="NewGroupName";

foreach($_SESSION[cart_array] as $row) {        

   if ($row['groupId'] == "26141"){
     echo "The initial GroupName is" . $row['GroupName'] . "<br>";

     echo   "The GroupName should be " . $NewGroupName."<br>";

     $row['GroupName']   = $NewGroupName;
     echo   "The actual GroupName is " . $row['GroupName']."<br>";


   }
}
print_r($_SESSION[cart_array]);
?>

The first print_r:

Array ( [0] => Array ( [groupId] => 26141 [GroupName] => 'Crystal Farm - Ten Yard Case Pack' [StylePatternColor] => A-CF-10 [Price] => 5.65 [StandardPutUp] => 320 [Discount] => 0 [DiscountText] => [StkUnit] => YDS [ListPrice] => 5.65 [Quantity] => 1 [PromiseDate] => 10/01/2017 [DoNotShipBefore] => 02-01-2017 [ColorName] => 32 Ten Yard Bolts [PatternName] => [SKUDescription] => [KitPerYardDiscount] => False [KitPerYardDiscountText] => False [Kit] => False ) [] => Array ( [DoNotShipBefore] => ) ) 

The assingment seems to work:

The initial GroupName is'Crystal Farm - Ten Yard Case Pack'
The GroupName should be NewGroupName
The actual GroupName is NewGroupName

But, the final print_r shows we have not changed the value of GroupName.

 Array ( [0] => Array ( [groupId] => 26141 [GroupName] 
=> 'Crystal Farm - Ten Yard Case Pack' [StylePatternColor] 
=> A-CF-10 [Price] 
=> 5.65 [StandardPutUp] 
=> 320 [Discount] 
=> 0 [DiscountText] 
=> [StkUnit] 
=> YDS [ListPrice] 
=> 5.65 [Quantity] 
=> 1 [PromiseDate] 
=> 10/01/2017 [DoNotShipBefore] => 02-01-2017 [ColorName] 
=> 32 Ten Yard Bolts [PatternName] => [SKUDescription] => [KitPerYardDiscount] => False [KitPerYardDiscountText] => False [Kit] => False ) [] 
=> Array ( [DoNotShipBefore] => ) )

Any help would be appreciated.

Leo
  • 7,274
  • 5
  • 26
  • 48
  • Duh `$_SESSION[cart_array]` >>> change to `$_SESSION['cart_array']` – RiggsFolly Jun 30 '17 at 19:26
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jun 30 '17 at 19:27

2 Answers2

4

You don't change $_SESSION anywhere in your code. The foreach just exposes a copy of each element. You could change it by using a reference &:

foreach($_SESSION['cart_array'] as &$row) {

Also, notice that quotes are required for string indexes $_SESSION['cart_array']. If you had error reporting on you would see a Notice for Undefined constant: cart_array.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

From php.net:

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Adam Forbis
  • 461
  • 3
  • 11