I am currently trying to parse an xml document with php. All is going well apart from one thing. Here is a sample of my xml:
<properties>
<property>
<propertyid>1</propertyid>
<flags>
<flag>This is a flag</flag>
<flag>This is another flag</flag>
<flag>This is yet another flag</flag>
<flag>etc...</flag>
</flags>
</property>
<property>
...
</property>
<properties>
As you can see there are multiple <property> nodes. I am using SimpleXML which works perfectly fine. I just loop through each <property> with php and get the values, for example:
foreach ( $xml->$property as $property )
{
echo $property->propertyid;
}
The problem is the <flag> nodes. Basically, I want to end up with JSON that looks like this:
{
"flags1":
{
"flag": "This is a flag"
},
"flags2":
{
"flag": "This is another flag"
},
"flags3":
{
"flag": "This is yet another flag"
}
,
"flags4":
{
"flag": "..."
}
}
There will be an indeterminate amount of flags for each property. I have tried a foreach loop to get the flag values which worked but then how can I get the values looking like the example JSON?
My foreach loop looks like this:
$flags = $property->flags;
foreach ( $flags->flag as $index => $flag )
{
$arr = array($index => (string)$flag);
echo json_encode($arr);
}
Which returns:
{"flag":"This is a flag"}{"flag":"This is another flag"}{"flag":"This is yet another flag"}{"flag":"etc..."}
This is so nearly there, I just don't know how to get it to be correct.