Slice a mulitdimensional array?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am building a webshop and each time the customer places a product in
the cart (a PLAcement) I add something to the $_SESSION variable:

$_SESSION[pla][$pla_id][product] = "key";
$_SESSION[pla][$pla_id][profile] = "FS632";

As he places more products in the cart the $pla_id gets incremented.

The customer also needs to be able to delete a placement from the
cart. Is it possible the slice out a $pla_id from the array and
resequence the placement id?

For example: when I slice out the third placement, then the fourth
placement should become the third placement.



Re: Slice a mulitdimensional array?

Louis wrote:
Quoted text here. Click to load it

Yes, but first a couple of comments on your code.  In your development
system, you should ALWAYS run with

error_reporting=E_ALL (or E_ALL | E_STRICT)

This will give you E_NOTICE errors in your code

$_SESSION[pla][$pla_id][product] = "key";

etc. because your strings need to be enclosed in quotes, i.e.

$_SESSION['pla'][$pla_id]['product'] = "key";

You can call unset() to remove an array element, i.e.


You really shouldn't depend on the indexes being sequential unless
absolutely necessary - you can use foreach() to go through each element
of the array, i.e.

foreach($_SESSION['pla'] as $item)

will give you each of the items in your list.  However, if you really
need to reorder the items, you can do something like:

$temp = array();
foreach ($_SESSION['pla'] as $item)
   $temp[] = $item;
$_SESSION['pla'] = $temp;

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Slice a mulitdimensional array?

Hello Jerry,

Thank you. I will really look into all of the points you are


On Fri, 22 May 2009 06:49:14 -0400, Jerry Stuckle

Quoted text here. Click to load it

Site Timeline