|
|
|
|
![]() ![]() |
Nov 9 2007, 02:44 PM
Post
#1
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 myCENTs:35.43 |
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 |
|
|
|
Nov 9 2007, 04:39 PM
Post
#2
|
|
|
Premium Member Group: Members Posts: 219 Joined: 13-February 07 Member No.: 20,371 |
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.
|
|
|
|
Nov 9 2007, 06:15 PM
Post
#3
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
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 |
|
|
|
Nov 9 2007, 09:38 PM
Post
#4
|
|
|
Absolute Newbie Group: Admin Posts: 888 Joined: 20-February 05 From: Indianapolis, Indiana, USA (Midwest) Member No.: 2,714 myCENTs:35.43 |
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 |
|
|
|
Nov 9 2007, 11:40 PM
Post
#5
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
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 |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 5th December 2008 - 02:47 PM |