Page 1 of 1

Merging two arrays

Posted: Sun Nov 19, 2006 9:57 pm
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

Posted: Sun Nov 19, 2006 11:24 pm
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.

Posted: Sun Nov 19, 2006 11:48 pm
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

Posted: Mon Nov 20, 2006 8:40 pm
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

Posted: Fri Nov 24, 2006 5:01 pm
by klarinetking
Just bumping this.

Does anyone have any ideas?

Thanks,

klarinetking

Posted: Sun Dec 03, 2006 6:14 pm
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