How to append an associative array to the end of another array?

Hi,

I am working on my cart for a project and having issue. I have really tried to sort it out myself but
same issue. Basically people will select shoes size from a dropdown menu to be sent to the cart. and
the code will check :

[ul][li]-if there is already value in the session cart : if same value for id and size, then we increase the qty
of the existing value[/li]
[li][/li][/ul]

[ul][li]-if there is already value in the session cart : if the id value is same, but the size value different, then
it is a new value, append to the existing value.[/li]
[li][/li][/ul]

For the first check the code work fine, it checks the existing values in the session cart and if it is the
same id and same size, the qty increase fine.

[php]the issue is in the second check: when i select another size in the dropdown but with the same
item id, the new value (id and size) just erase the existing values in cart.[/php]

What i want is that: if item id is same, but has a different size, then it is a new value and that array
value need to be appended to end of the session cart as a new value

[php]<?php
$items = array(
array(‘id’ => ‘1’, ‘desc’ => ‘sneaker’,‘price’ => 24.95, ‘size’=>‘424546’),
array(‘id’ => ‘2’, ‘desc’ => ‘Reebok’,‘price’ => 200, ‘size’=>‘323845’),
array(‘id’ => ‘3’, ‘desc’ => ‘Songs of the Goldfish (2CD set)’,‘price’ => 19.99),
array(‘id’ => ‘4’, ‘desc’ => ‘Simply JavaScript (SitePoint)’, ‘price’ => 39.95)
);

session_start();
if (!isset($_SESSION[‘cart’])) {
$_SESSION[‘cart’] = array();
}

if (isset($_POST[‘action’]) and $_POST[‘action’] == ‘Buy’) {
$size="";
//Get the values for the post and make sure they are integers
$pid = filter_var( $_POST[‘id’], FILTER_VALIDATE_INT, array(‘min_range’=> 1) );
$size = $_POST[‘doSize’];
if (isset($_SESSION[‘cart’][$pid])) {

foreach ($_SESSION['cart'] as $key) {

if ($key[‘id’] === $pid and $key[‘size’] !== $size) {
echo “NOT SAME SIZE”;
$si = $size;
$_SESSION[‘cart’][$key[‘id’]] = array(‘id’=>$pid, ‘size’=>$si, ‘quantity’=>1);

  }

  elseif ( $key['id'] === $pid and $key['size']=== $size ) {
     $_SESSION['cart'][$key['id']]['quantity']++;
        echo "YES CORRECT";
  } 

}//end foreach
print_r($_SESSION[‘cart’]);
}//end isset session cart

else{
$_SESSION[‘cart’][$pid]= array(‘id’=>$pid, ‘size’=>$size, ‘quantity’=>1);

foreach ($_SESSION[‘cart’] as $key) {
echo $key[‘id’];
}

print_r($_SESSION[‘cart’]);
}
}//END POST
include’cat.html’;

//unset($_SESSION[‘cart’]);
?>[/php]

Here the cart.html

[php]

Product catalog table { border-collapse: collapse; } td, th { border: 1px solid black; }

Your cart contains <?php echo count($_SESSION['cart']);?> item(s).

View your cart

<?php foreach ($items as $item): ?> <?php endforeach; ?>
Item Description Price Size
<?php echo $item['desc']; ?> $<?php echo number_format($item['price'], 2); ?> <?php if (isset($item['size'])) { ?> <?php foreach(str_split($item['size'], 2) as $size):?> <?php echo $size;?> <?php endforeach;?> <?php }?>

All prices are in imaginary dollars.

[/php]

Without actually testing your code, I can see one thing that is probably overwriting the other products.
Your $_SESSION var is being set with “cart” and “key” as array keys but then is just assigning that to a value
[php]$_SESSION[‘cart’][$key[‘id’]] = array(‘id’=>$pid, ‘size’=>$si, ‘quantity’=>1); [/php]

I can see there is another key of “quantity” which is being incremented, so I assume you just need to instead assign the new item to a new key and make sure that is an array e.g

[php]
// Add your conditional check for same ID different size here…
// Make sure the key value is an array
if (!isset($_SESSION[‘cart’][$key[‘id’]][‘items’])) {
$_SESSION[‘cart’][$key[‘id’]][‘items’] = array();
}

// Note the blank [] at the end to signify a new array value. This could be something unique to the product i.e size+id if
// think you’ll need to reference it again.
$_SESSION[‘cart’][$key[‘id’]][‘items’][] = array(‘id’=>$pid, ‘size’=>$si, ‘quantity’=>1);
[/php]
Hope that helps.

Sponsor our Newsletter | Privacy Policy | Terms of Service