This question already has an answer here:
- Pretty-Printing JSON with PHP 22 answers
I've seen multiple answers in here but none of them had a solution to my question, so I made an account to ask this question. I understand now that
is not an allowed character in json because the backslash is not allowed and that's why the problem is occurring.
I have the following code to encode an array in json:
<?php
$data = array('test1' => 'something1', 'test2' => 'something2', 'test3' => 'something3');
echo json_encode($data);
I'm trying to have the string outputted as follows:
{
"test1": "something1",
"test2": "something2",
"test3": "something3"
}
But what I'm getting is this:
{"test1":"something1","test2":"something2","test3":"something3"}
This is my go at it:
<?php
$data = array('test1' => 'something1
', 'test2' => 'something2
', 'test3' => 'something3
');
echo json_encode($data);
but this returns
{"test1":"something1 ","test2":"something2 ","test3":"something3 "}
</div>