We help IT Professionals succeed at work.

Combining 2 arrays of data into one

DAZTOMKINS
DAZTOMKINS used Ask the Experts™
on
Hi

I have 2 arrays of data, one is via an XML feed, I am using simplexml_load_file for this, and the other is from a MySQL database (using a prepared statement).

Currently I have them running independently but I am now at the stage where I want them combined and returned into one list on my application.

I would appreciate some guidance on this issue.

Thank you

Daz
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2004

Commented:
If you are using arrays indexed by numbers, you can use array_merge().

Author

Commented:
Ok thanks for that.

Both of my current data sources have various fields like Name, Price, Distance, etc.

If I use array_merge() how will the fields be accessed?

Top Expert 2004

Commented:
String keys in your arrays will overwrite one another. But I am curious, what is the layout of both your arrays exactly?

Author

Commented:
Well as I said one of the arrays is simple from a mysql database, it has various column names like Name, Price, etc.

The other isnt an array yet but I was going to make it one, but it is coming from an xml feed, and the data is stored like this.

<listing>
<name>Test</name>
<price>10.00</price>
</listing>

<listing>
<name>Hello</name>
<price>26.00</price>
</listing>

I am using a foreach statement to get each listing from the xml feed and display it on my page.

My Mysql database has similar column names. I want to combine the data, presumably into an array, and then display it in one list on my page, please tell me if you think there is a better way to do it.

Thanks.
Top Expert 2004

Commented:
Correct me if I am wrong, but in your case you have not yet created the two arrays that each hold listings. What you can then do is create a general $listings = array(); that is numerically indexed. In your foreach loop you can add to each individual listing to the $listings array. The first listing goes in $listings[0]. Do the same for your XML.

Author

Commented:
Hi, Thanks for the response and your help so far.

I understand what you are suggesting but not 100% sure on how to implement it.

I know how to start the array using $listings = array(); but not sure how to put the data from each column in my Mysql database into the array, and likewise im not sure how to then get the data back out of the array?
Top Expert 2004

Commented:
For instance,

You can initialize at the top $listings = array(); Then, you can work with the following (pseudo) code for MySQL:
$count = count($listings);
foreach ($row = mysql_fetch_array($result)) {
    $listings[$count] = array();
    $listings[$count]["Field1"] = "Field 1";
    // ...
    ++$count;
}

Open in new window

Similarly for your XML.

Author

Commented:
Hi

Thanks for that, it was helpful.

That puts the data into the array, so how do I get it out?

Daz
Top Expert 2004

Commented:
Each array element of $listings is an array containing information for that particular listing. To get the "name" field of the 4th listing, for example, you would use $listings[3]["name"].

Author

Commented:
Ok. Say I didnt watch to specifically get the name field of the 4th listing but instead wanted to run through all of the listings that are now stored in the array. Can I use a foreach statement to loop through each listing in my new array?
Top Expert 2004

Commented:
Yes, you can use a foreach statement in that case.

Author

Commented:
Thank you for this, it is now working great. Can you tell me how to sort the array, I have read about the 'asort' function but this isn't exactly what I want. I want to sort by one of the fields, the price one in particular.
Top Expert 2004

Commented:
Please check out Example 3 in the documentation for array_multisort(), which may be similar to what you want.

Author

Commented:
Hi, Thanks, I have now used the array_merge function that you suggested to me at the beginning of this thread, it has worked great and my 2 data sources are now combined in the one array, however at the moment when I view the results on screen the first arrays data is at the top and then the second arrays data is underneath, my question above was asking how I can now re-sort this newly merged array by one of its fields, the price field. I have just looked at the function you mentioned 'array_multisort()' but it doesnt seem to fit this purpose. Do you have any other ideas?

Thanks again for your help so far.
Top Expert 2004
Commented:
Are you sure Example 3 doesn't fit your purpose? It seems to if I understand the organization of your array.

Author

Commented:
Yes actually I got it working. Sorry for the confusion. Thanks very much for your help today, I appreciate it.