Adding Array to Array

Hi all, I have a shopping cart for a mobile repair company, when a user adds something to their basket, the basic details of the phone are added, then the actual repair (such as a screen replacement, new battery, etc) are added under the options element of the array. How can I add to the options array when a customer adds another repair to their basket for the same phone? Hope I’ve explained well enough…

$item = array(
			'id' => $product->repair_id,
			'qty' => 1,
			'price' => 0.00,
			'name' => $product->name.' '.$product->repair_name,
			'img' => $product->repair_image,
			'link' => 'fix/'.$product->url,
			'options' => array('price' => 5.60, 'name' => 'Screen'), array('price' => 5.60, 'name' => 'New Battery')
		);
$item['options'][] = array('price' => 5.60, 'name' => 'New Battery');

Hi, awesome thank you so much! Goodness knows why I didn’t use this, brain freeze!

Taken from http://us3.php.net/array_push

[INDENT] bart at framers dot nl 26-Sep-2001 10:16 Array_push also works fine with multidimensional arrays. Just make sure the element is defined as an array first.

<?php
$array[“element”][$element][“element”] = array();
array_push ($array[“element”][$element][“element”], “banana”);
?>

[/INDENT]

Won’t this overwrite all of options?

No. It adds to a new entry to the array. “” acts like auto-increment ID in MySQL.

Correct, though I like to think of the invoking array_push automatically for you :wink:

Side note: What you’ve defined there is not a multidimensional array. I -think- you did this just as an example, but what you meant was an array of arrays…

			'options' => array(array('price' => 5.60, 'name' => 'Screen'), array('price' => 5.60, 'name' => 'New Battery'))

The initial code would have looked like:


$item = array(
            'id' => $product->repair_id,
            'qty' => 1,
            'price' => 0.00,
            'name' => $product->name.' '.$product->repair_name,
            'img' => $product->repair_image,
            'link' => 'fix/'.$product->url,
            'options' => array('price' => 5.60, 'name' => 'Screen'), 
            0 => array('price' => 5.60, 'name' => 'New Battery')
        );