DAZTOMKINS
asked on
Combining 2 arrays of data into one
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
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
If you are using arrays indexed by numbers, you can use array_merge().
ASKER
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?
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?
String keys in your arrays will overwrite one another. But I am curious, what is the layout of both your arrays exactly?
ASKER
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.
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.
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.
ASKER
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?
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?
For instance,
You can initialize at the top $listings = array(); Then, you can work with the following (pseudo) code for MySQL:
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;
}
Similarly for your XML.
ASKER
Hi
Thanks for that, it was helpful.
That puts the data into the array, so how do I get it out?
Daz
Thanks for that, it was helpful.
That puts the data into the array, so how do I get it out?
Daz
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"].
ASKER
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?
Yes, you can use a foreach statement in that case.
ASKER
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.
Please check out Example 3 in the documentation for array_multisort(), which may be similar to what you want.
ASKER
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.
Thanks again for your help so far.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes actually I got it working. Sorry for the confusion. Thanks very much for your help today, I appreciate it.