Forum Moderators: coopster

Message Too Old, No Replies

How can I adapt this code?

         

egibberate

8:47 pm on Mar 13, 2009 (gmt 0)

10+ Year Member



Hi,
I this code works fine where 'park' has an attribute. However, I have chosen to remove the attributes from the 'park' elements. As an absolute beginner at xml and php I can't figure how to eliminate properly the attributes content from the code below. Could some kind expert show me how to modify it. Thank you.

$sxml = new SimpleXMLElement($doc);
foreach ($sxml->children() as $park) {
$key = (string) $park->attributes()->number;
$data[$key]['sheep'] = (string) $park->sheep;
$data[$key]['horse'] = (string) $park->horse;
$data[$key]['dog'] = (string) $park->dog;
$data[$key]['cow'] = (string) $park->cow;
}
print_r($data);

penders

4:37 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



SimpleXML [uk3.php.net] is just one object that can be used to manipulate XML in PHP.

Does your 'park' element still have the 4 children: sheep, horse, dog and cow?

Then if you have 0 attributes...

foreach ($sxml->children() as $park) {  
$data[0]['sheep'] = (string) $park->sheep;
$data[0]['horse'] = (string) $park->horse;
$data[0]['dog'] = (string) $park->dog;
$data[0]['cow'] = (string) $park->cow;
}

(?) You might as well get rid of the first array dimension [0] as well, but that will depend on the rest of your code. (And you could step through the children() of 'park' rather than specify the elements directly.)