Welcome to WebmasterWorld Guest from 54.197.116.116

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)

5+ Year Member



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:


<?php
$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)

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



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)

5+ Year Member



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

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

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.