I have the following XML string:
<catalog>
<language>
<name>Php</name>
<type>Server Side</type>
</language>
<language>
<name>JavaScript</name>
<type>Client Side</type>
</language>
<language>
<name>Asp</name>
<type>Server Side</type>
</language>
</catalog>
I need to parse it and convert it into JSON, using PHP.
This is what I tried:
$xml = simplexml_load_string($xmlData);
foreach ($xml->language as $lang) {
$json[] = [
'name' => $lang->name,
'type' => $lang->type
];
}
var_export(json_encode($json));
Result:
'[{"name":{"0":"Php"},"type":{"0":"Server Side"}},{"name":{"0":"JavaScript"},"type":{"0":"Client Side"}},{"name":{"0":"Asp"},"type":{"0":"Server Side"}}]'
As you can see, the value of each property in an object - while I need this:
'[{"name":"Php","type":"Server Side"},{"name":"JavaScript","type":"Client Side"},{"name":"Asp","type":"Server Side"}]'
What's wrong in my code?
What I have tried:
$xml = simplexml_load_string($xmlData);
foreach ($xml->language as $lang) {
$json[] = [
'name' => $lang->name,
'type' => $lang->type
];
}
var_export(json_encode($json));