I apologize if this question seems foolish, but I am struggling to figure things out. My goal is to extract specific values from an XML file and convert them into a JSON array within my PHP script.
Here is how I am currently fetching data from the XML file and displaying the JSON output based on different cases:
switch ($creditId) {
case 123:
$xml = new XMLReader;
$xml->open('123.xml');
$doc = new DOMDocument;
while ($xml->read() && $xml->name !== 'Decision');
while ($xml->name === 'Decision')
{
$node = simplexml_import_dom($doc->importNode($xml->expand(),
true));
$reason_text = $node->Reason->ReasonText ;
echo "$reason_text \n";
// move to the next <Decision>
$xml->next('Decision');
}
echo '[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo" ,
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]';
break;
case 789:
$xml = new XMLReader;
$xml->open('789.xml');
$doc = new DOMDocument;
while ($xml->read() && $xml->name !== 'Decision');
while ($xml->name === 'Decision')
{
$node = simplexml_import_dom($doc->importNode($xml->expand(),
true));
$reason_text = $node->Reason->ReasonText ;
echo "$reason_text \n";
// move to the next <Decision>
$xml->next('Decision');
}
echo '[{
"creditId": 789,
"client": "Jonas",
"Decision": "Random",
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]';
break;
default:
http_response_code(404);
}
The placeholders REASONTEXT1 AND REASONTEXT2 are where the $reason_text variable values should be displayed in the JSON array. For example, the current JSON output for case 123 displays RandomReason1 and RandomReason2 as follows:
RandomReason1
RandomReason2
[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo",
"Factors": ["REASONTEXT1","REASONTEXT2"]
}]
My desired outcome is to have the values displayed like this:
[{
"creditId": 123,
"client": "Peter",
"Decision": "Solo",
"Factors": ["RandomReason1","RandomReason2"]
}]
In conclusion, I am looking to transform specific XML data into a JSON array. Thank you for your assistance!