你们能帮我解释一下这里发生了什么吗?
$data[0] = array("one" => "uno", "two" => "dos", "three" => "tres");
$data[1] = array("one" => "uno", "two" => "dos", "three" => "tres");
//unset($data[0]);
$encode = json_encode($data);
$decode = json_decode($encode);
var_dump($decode);
输出:
array(2) { [0]=> object(stdClass)#1 (3) { ["one"]=> string(3) "uno" ["two"]=> string(3) "dos" ["three"]=> string(4) "tres" } [1]=> object(stdClass)#2 (3) { ["one"]=> string(3) "uno" ["two"]=> string(3) "dos" ["three"]=> string(4) "tres" } }
这是正常的,将它保留为一个数组,但是一旦我取消设置数组的一部分,它就会将它变成一个 obj。
$data[0] = array("one" => "uno", "two" => "dos", "three" => "tres");
$data[1] = array("one" => "uno", "two" => "dos", "three" => "tres");
unset($data[0]);
$encode = json_encode($data);
$decode = json_decode($encode);
var_dump($decode);
输出:
object(stdClass)#1 (1) { ["1"]=> object(stdClass)#2 (3) { ["one"]=> string(3) "uno" ["two"]=> string(3) "dos" ["three"]=> string(4) "tres" } }
如何保持一致性?