I've stubmled across this behavior on PHP 5.6 (also identical in PHP 5.4 up to 7.0).
$note = new SimpleXMLElement('<Note></Note>');
$note->addChild("string0", 'just a string');
$note->addChild("string1", "abc\n\n\n");
$note->addChild("string2", "\tdef");
$note->addChild("string3", "\n\n\n");
$note->addChild("string4", "\t\n");
$json = json_encode($note, JSON_PRETTY_PRINT);
print($json);
Outputs:
{
"string0": "just a string",
"string1": "abc\n\n\n",
"string2": "\tdef",
"string3": {
"0": "\n\n\n"
},
"string4": {
"0": "\t\n"
}
}
There must be a reason behind this behavior, I would like to understand. And also, if you know of a way to force it to behave the same way for strings of texts and whitespace I would appreciate you sharing your ideas!
Edit. Here's a snippet you can run: http://sandbox.onlinephpfunctions.com/code/d797623553c11b7a7648340880a92e98b19d1925