syn3rgy Posted March 16, 2011 Report Share Posted March 16, 2011 As I have been going through the tutorials, I wanted to try and use what I'm learning.I came across the need to export values from an XML file to a CSV file. Looking at examples in the PHP manual and trying to use what I have been learning I have started the script. The XML layout looks like this: <?xml version="1.0" encoding="utf-8"?> <form> <title><![CDATA[submit Directory Profile Information]]></title> <submissions> <submission> <name><![CDATA[Joe Mama]]></name> <email><![CDATA[enter@here.com]]></email> <address><![CDATA[1234 there]]></address> <city><![CDATA[Thereville]]></city> <state><![CDATA[iL]]></state> <zip>55566</zip> <phone><![CDATA[555-555-1234 x1234]]></phone> <fax><![CDATA[555-555-5678]]></fax> <website><![CDATA[http://]]></website> </submission> <submission> <name><![CDATA[Test Item]]></name> <email><![CDATA[enter@here.com]]></email> <address><![CDATA[1234 there]]></address> <city><![CDATA[Thereville]]></city> <state><![CDATA[iL]]></state> <zip>55566</zip> <phone><![CDATA[555-555-1234 x1234]]></phone> <fax><![CDATA[555-555-5678]]></fax> <website><![CDATA[http://]]></website> </submission> </submissions> </form> The beginning of my code looks like this: $file = "mystuff.xml"; $xml = simplexml_load_file($file); //get data from XML tags foreach($xml->submissions->submission as $item) { //store those in array $basic = array( "name" => (string)$item->name, "email" => (string)$item->email, "address" => (string)$item->address, "city" => (string)$item->city, "state" => (string)$item->state, "zip" => (string)$item->zip, "phone" => (string)$item->phone, "fax" => (string)$item->fax, "website" => (string)$item->website ); Not very far and my first problem appears. I understand why it happens I just don't know how to fix it. For every foreach loop the result will just overwrite the the first one. So if I vardump outside the loop you get one submission, not both. I'm stuck because I can't figure out how to get the results from each loop outside the loop, if that makes sense. Either its not very easy to do, or I am just missing something simple, either way if I can get some help with that I can move one to the next part. Thanks for the time, benjmain Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 16, 2011 Report Share Posted March 16, 2011 The solution is to use a 2D array -- an array that contains multiple arrays. For example, the first set of values would be: $basic[0]["name"] $basic[0]["email"] ...etc and the second record would be $basic[1]["name"] $basic[1]["email"] ...etc You would need to create a temporary variable that started at "0" and 1 was added to the value every time PHP loops through the foreach array (I'll call it $i for the sake of this example.) With that in place, you could do something like this: $basic[$i]["name"] = (string)$item->name; $basic[$i]["email"] = (string)$item->email; ...etc Do a Google search or two on "2d arrays" if you're having trouble. I can put together a basic example a bit later today when I have the time. This site has a bit more about 2D arrays (near the bottom of the page): http://www.learnphp-tutorial.com/Arrays.cfm Quote Link to comment Share on other sites More sharing options...
syn3rgy Posted March 16, 2011 Author Report Share Posted March 16, 2011 Wow doh! I have no idea why I didn't think of laying out the array that way. This is what I have now: $file = "rsform.xml"; $xml = simplexml_load_file($file); $i = 0; //get data from XML tags foreach($xml->submissions->submission as $item) { //store those in array for later $basic[$i]["name"] = (string)$item->name; $basic[$i]["email"] = (string)$item->email; $basic[$i]["address"] = (string)$item->address; $basic[$i]["city"] = (string)$item->city; $basic[$i]["state"] = (string)$item->state; $basic[$i]["zip"] = (string)$item->zip; $basic[$i]["phone"] = (string)$item->phone; $basic[$i]["fax"] = (string)$item->fax; $basic[$i]["website"] = (string)$item->website; //increment the index ++$i; } var_dump($basic); Which of course dumps an array with an array inside at index 0 and 1. array(2) { [0]=> array(9) { ["name"]=> string(8) "Joe Mama" ["email"]=> string(14) "enter@here.com" ["address"]=> string(10) "1234 there" ["city"]=> string(10) "Thereville" ["state"]=> string(2) "IL" ["zip"]=> string(5) "55566" ["phone"]=> string(18) "555-555-1234 x1234" ["fax"]=> string(12) "555-555-5678" ["website"]=> string(7) "http://" } [1]=> array(9) { ["name"]=> string(9) "Test Item" ["email"]=> string(14) "enter@here.com" ["address"]=> string(10) "1234 there" ["city"]=> string(10) "Thereville" ["state"]=> string(2) "IL" ["zip"]=> string(5) "55566" ["phone"]=> string(18) "555-555-1234 x1234" ["fax"]=> string(12) "555-555-5678" ["website"]=> string(7) "http://" } } Again thanks, I will continue on my script and post back if I need more help. A week into PHP and its already beating me up! The solution is to use a 2D array -- an array that contains multiple arrays. For example, the first set of values would be: $basic[0]["name"] $basic[0]["email"] ...etc and the second record would be $basic[1]["name"] $basic[1]["email"] ...etc You would need to create a temporary variable that started at "0" and 1 was added to the value every time PHP loops through the foreach array (I'll call it $i for the sake of this example.) With that in place, you could do something like this: $basic[$i]["name"] = (string)$item->name; $basic[$i]["email"] = (string)$item->email; ...etc Do a Google search or two on "2d arrays" if you're having trouble. I can put together a basic example a bit later today when I have the time. This site has a bit more about 2D arrays (near the bottom of the page): http://www.learnphp-tutorial.com/Arrays.cfm Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.