Nov 22, 2009

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

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

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

vujsa
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:
CODE
$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:
CODE
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

 

 

 


Comment/Reply (w/o sign-up)

.:Brian:.
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.

Comment/Reply (w/o sign-up)

vizskywalker
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

Comment/Reply (w/o sign-up)

vujsa
QUOTE(vizskywalker @ Nov 9 2007, 01:15 PM) *
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:
CODE
$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

 

 

 


Comment/Reply (w/o sign-up)

vizskywalker
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

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Merge Unknown Arrays Litle

  1. Php Mysql Errors - Fetching arrays (2)



Looking for merge, unknown, number, arrays, litle,

See Also,

*SIMILAR VIDEOS*
Searching Video's for merge, unknown, number, arrays, litle,
advertisement



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

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com