Updating array with array_push is not working

Hi.

I am trying to either set an array or update the current array that is held within a session - for some reason it isnt working and is throwing a error (Warning: array_push() expects parameter 1 to be array, integer given in C:\xampp\htdocs\site\folder\cart.php on line 41)

This is the code


if(empty($_SESSION['cart']))
			{
				echo "empty session <br>";
				$cartData = array('itemId' => $articleId, 'itemTitle' => $articleTitle);
				$_SESSION['cart'] = $cartData;
			}
		else
			{
				echo "full session <br>";
				$cartData = $_SESSION['cart'];
				$cartData = array_push($cartData,$articleId,$articleTitle);
				$_SESSION['cart'] =$cartData;
			}
			
				echo "The END: <br>";
				echo "<pre>";
				print_r($_SESSION['cart']);
				echo "</pre>";
		
		
		
		//echo "<pre>";
		//echo $where . "<br>";
		//print_r($_SESSION['cart']);
		//echo "</pre>";

so far its very simple, I am checking to see if the session is set and if not I create the inital array and assign it to the session there after I should just be pushing into the array and updating the session but as said it throwing the above error.

What am I doing wrong!

Cheers
Chris

array_push doesn’t return the array. It modifies the array and returns the number of new elements.

Try this


if(empty($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
    echo "empty session <br>";
}
$item = array('itemId' => $articleId, 'itemTitle' => $articleTitle);
array_push($_SESSION['cart'], $item); // alternative: $_SESSION['cart'][] = $item;

echo "The END: <br>";
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";

:cool:

Oops… I had even read the manual and missed that!

Next question then, How do I keep the named index like ‘itemId’ => $articleId, ‘itemTitle’ => $articleTitle as array_push doesnt like that syntax or does it not allow it all… from my reading I cant find away.

Thanks again
Chris

As is, the keyed values should remain intact, and array_push will add the new items using numeric indexes.