I am converting from JSON to object and from object to array. It does not what I expected, can you explain to me?
$json = '{"0" : "a"}';
$obj = json_decode($json);
$a = (array) $obj;
print_r($a);
echo("a0:".$a["0"]."<br>");
$b = array("0" => "b");
print_r($b);
echo("b0:".$b["0"]."<br>");
The output here is:
Array ( [0] => a ) a0:
Array ( [0] => b ) b0:b
I would have expected a0:a at the end of the first line.
Edit: After reading the answers I extended the code, which makes the behaviour more clear:
//extended example
$json = '{"0" : "a"}';
$obj = json_decode($json);
$a = (array) $obj;
var_export($a);
echo("a0:".$a["0"]."<br>"); //this line does not work, see the answers
echo $obj->{"0"}."<br>"; //works!
$json = '{"x" : "b"}';
$obj = json_decode($json);
$b = (array) $obj;
var_export($b);
echo("bx:".$b["x"]."<br>");
$c = array("1" => "c");
var_export($c);
echo("c1:".$c["1"]."<br>");
$d = array("0" => "d");
var_export($d);
echo("d0:".$d["0"]."<br>");
Output of extended example:
array ( '0' => 'a', )a0:
a
array ( 'x' => 'b', )bx:b
array ( 1 => 'c', )c1:c
array ( 0 => 'd', )d0:d