Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Need to populate array (with keys) from for each loop

Populate array from xml file

8:33 pm on Nov 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0

Hey All,
This should be pretty easy, but I am just a bit new to this part of the game.. :) I have a simple xml file that I am loading and using to load different page content (I get the XML file from others in our group pre-filled). Anyway, I wrote the following:

$xml = simplexml_load_file("info.xml");
$xmlarray = array();
foreach($xml->children() as $child)
$xmlarray[] = $child;

This works fine, but now it has come up that I need to check the field names, as we can have slight variations due to # of bullets needed/etc...

I assume the easiest way to handle this would be to also assign the xml field names as keys into the array and then check against that in php. This is where I am stuck, trying to grab those field names. Ultimately the array values would be something like:

$xmlarray('field1' => 'value')

am I on the right track here? Any help is appreciated.
9:05 pm on Nov 28, 2011 (gmt 0)

Senior Member from CA 

WebmasterWorld Senior Member httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 29, 2003
votes: 0

not sure what you mean by "field names" in XML. Are these child nodes of each $child? Or are they the $child nodes themselves?

a sample of the XML you're parsing would help (just a small fragment would suffice)
9:29 pm on Nov 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Jan 18, 2008
posts: 73
votes: 0

Yes sorry, got the terminology wrong. So something like so...

<field1>This is the data for field 1</field1>
<field2>This is the data for field 2</field2>

So essentially I would want an array that would contain "field1" as the key as well as the data the node contains. Hope that makes more sense.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members