How to add a key value pair at the end of a multidimensional array?

I am working on array for a project and i want to know how i can add a key value pair at the end of an array. So here are the elements:

[php]1st array
$items = array(
array(‘id’ => ‘1’, ‘desc’=>‘Canadian-Australian Dictionary’, ‘price’=>24.95),
array(‘id’ => ‘2’, ‘desc’=>‘As-new parachute (never opened)’, ‘price’=> 1000),
array(‘id’=>‘3’, ‘desc’=>‘Songs of the Goldfish (2CD set)’, ‘price’=> 19.99)
);[/php]

[php]2nd array:
$cart = array(
array(‘id’ => ‘1’,‘quantity’=>2)
);[/php]

So Basically what to know is how i can add values from 1st array to 2nd array based on the id to have a array like this one.

final array i want to get:

[php]$itemDetail = array(
array(‘id’ => ‘1’, ‘desc’=>‘Canadian-Australian Dictionary’, ‘price’=>24.95, ‘quantity’=> 1)
);[/php]

Hope i expose my concern properly. Thanks

[php]

$items = array(
array(
‘id’ => ‘1’,
‘desc’ => ‘Canadian-Australian Dictionary’,
‘price’ => 24.95
),
array(
‘id’ => ‘2’,
‘desc’ => ‘As-new parachute (never opened)’,
‘price’ => 1000
),
array(
‘id’ => ‘3’,
‘desc’ => ‘Songs of the Goldfish (2CD set)’,
‘price’ => 19.99
)
);

echo ‘

’;
print_r($items);
echo ‘
’;

$cart = array(
array(
‘id’ => ‘1’,
‘quantity’ => 2
)
);

foreach ($cart as $key => $val ) {
$items[$val[‘id’]][‘quantity’] = $val[‘quantity’];
}

echo ‘

’;
print_r($items);
echo ‘
’;
[/php]

Hi astonecipher,

I have tried the solution you propose me but it is working the way i think…The solution does not select the right array id. because i want to sort the final array based on the inner array id.

This is what i am trying to achieve:
Here the
[php]$items = array(
array(
‘id’ => ‘1’,
‘desc’ => ‘Canadian-Australian Dictionary’,
‘price’ => 24.95
),
array(
‘id’ => ‘2’,
‘desc’ => ‘As-new parachute (never opened)’,
‘price’ => 1000
),
array(
‘id’ => ‘3’,
‘desc’ => ‘Songs of the Goldfish (2CD set)’,
‘price’ => 19.99
)
);

$cart = array(
array(
‘id’ => ‘1’,
‘quantity’ => 2
)
);[/php]

Then if the array [php]cart id[/php] is equal to an [php]id in item arra[/php]y… i should have one only array with the cart array quantity append to it.
for example:
the [php]$cart id 1[/php] is equal to [php]$items id 1[/php]…so the final array shoud looks like:

[php]$itemDetail = array(
array( ‘id’ => ‘1’, ‘desc’ => ‘Canadian-Australian Dictionary’, ‘price’ => 24.95, ‘quantity’=> 2 )
);[/php]

Thanks for the assistance

Sponsor our Newsletter | Privacy Policy | Terms of Service