Jump to content

how can I add the key and value of one array to the end of another array? newbie


kee2ka4

Recommended Posts

I have two arrays and I want to add the value of one array into another array. Here is the layout of both the arrays. The first array is called $res['id'], and has the value of 1. The second array is an array within an array and is called $routes, as shown below:

 

$routes = array(

array('url' => '/^(posts)?\/?(page\/)?(?P<page>\d?)$/', 'controller' => 'posts', 'view' => 'index'));

 

I wanted to add $res['id'] (including the keyname) to the end of the $routes array and after adding the frist array, the second array will look like:

 

$routes = array(

array('url' => '/^(posts)?\/?(page\/)?(?P<page>\d?)$/', 'controller' => 'posts', 'view' => 'index', 'id' => '1'));

 

How can I do this in php.

 

Thanks,

Ket

Does the $res array only hold the id? If so you could do

$routes[0] += $res;

 

However if $res holds more than one item, then you'll need to do

$routes[0] += array('id' => $res['id']);

 

Or alternatively (short/cleaner)

$routes[0]['id'] = $res['id'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.