Merging two arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Merging two arrays

Post by klarinetking »

Hi,

I'm trying to merge two arrays. Well, kind of. Basically, I'm assigning values to a multidimensional array

Code: Select all

<?php
$array['root'][1][6][0] = 'Some value';
?>
I have another array that contains at least one variable, and I want to insert the values in that array after the 'root' element.

For example, say this second array looks like this:

Code: Select all

<?php
$something = array('0' => 3, '1' => 7);
?>
I would like the original statement to look like this

Code: Select all

<?php
$array['root'][3][7][1][6][0] = 'Some Value';
?>
If anyone has any suggestion as to how this could be done, it would be appreciated.

Thanks everyone

klarinetking
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Look into array_merge() and array_merge_recursive().

Apart from that you should seriously consider not using six-dimensional arrays. You're making your life much more difficult and, probably, unnecessarily so. There should be an easier solution to what you're trying to achieve.

My personal rule of thumb is: arrays have a maximum of two dimensions, anything beyond that means that my code is too complicated. Simplify.
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Hi,

Thanks for your reply.

I've looked at array_merge, but it doesn't seem able to insert the values in the middle of the array. Or maybe I just missed something in the documentation.

And I'm not really using 6 dimensional arrays. I'm trying to find a way to store an unlimited number of categories in an array. I'm trying to build the array automatically, which is why I need to insert values in the middle.

Actually, writing this has given me an idea to try out.

Thanks patrikG!

klarinetking
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Ok, so that didn't work. Does anyone know how I could solve my problem, or if anyone has a better idea. If you need to see code or anything, just let me know.

Thanks so much

klarinetking
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Just bumping this.

Does anyone have any ideas?

Thanks,

klarinetking
klarinetking
Forum Commoner
Posts: 59
Joined: Mon Jul 24, 2006 9:43 am

Post by klarinetking »

Hi,

Just another bump. It seems like this idea is too complicated, so I'm trying to simplify. If anyone has any ideas, please let me know.

Thanks.

klarinetking
Post Reply