Jump to content

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


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'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.