Jump to content
xisto Community
vujsa

What Is The Best Way To Merge An Unknown Number Of Arrays? Looking for a litle help with this one.

Recommended Posts

I have been working on a project and attempted several methods of returning the data I need but using array_merge() then count_array() to return matching items from a number of arrays seems to work the best.

As a result, I could have hundreds of arrays returned by a database query that I would need to merge. The arrays would have a structure like this:

$result[0] = array('item1', 'item2', 'item3');$result[1] = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6');$result[2] = array('item1', 'item2', 'item3', 'item4');$result[3] = array('item1', 'item2');

So I would need a script that would do the following dynamically:
array_merge($result[0], $result[1], $result[2], $result[3]);

I've got a few ideas of how I could do this but each idea will take time to code, test, and compare to other options. I was hoping that somebody already knew a quick way to do this.

Thanks,

vujsa

Share this post


Link to post
Share on other sites

Well, the method that comes to mind for me (which I am sure you have thought of) is the basic, go through each array one by one....and add the items to a new array. The only problem with this method is that the runtime of it is quite long...as you have to go through your array multiple times...and so i got to think that there is some better solution out there...i'll think about it a bit and see if I can come up with any ideas.

Share this post


Link to post
Share on other sites

Well, if you can get your code to return an array of the arrays to be merged, then it is really simple to pass them in as arguments to array merge. Look at the call_user_func_array.

 

~Viz

Thanks, that worked great.

 

I wasn't sure what you wanted me to do at first but I read through a few user contributed notes for array_merge and found what you were referring to.

 

Ended up with this:

$result[0] = array('item1', 'item2', 'item3');$result[1] = array('item1', 'item2', 'item3', 'item4', 'item5', 'item6');$result[2] = array('item1', 'item2', 'item3', 'item4');$result[3] = array('item1', 'item2');$merged_items = call_user_func_array('array_merge', $result);

This saved me a bunch of time as I wouldn't have stumbled over this by myself for days if ever...

 

I was about to do a loop to merge the arrays the hard way.

 

Thanks again,

 

vujsa

Share this post


Link to post
Share on other sites

No problem. It's not the most obvious of functions. I only stumbled across it recently when I was working with the object oriented mysqli interface and wanted to obtain an associative array of the results. A comment by someone at the bottom demonstrated how, using this function, so I wasn't clear on what his code did. I looked up the function, and it turns out it is quite useful.~Viz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.