JSON数组未设置

时间:2015-11-10 18:12:14

标签: php json database

首先,我真的不知道是否存在这种话题。但我搜索了很多,现在我在这里。

关于解析的问题。例如,我想取消一些项目。

$now = array();
$now[0]['name'] = "Hello1";
$now[0]['si'] = "BumBum1";
$now[1]['name'] = "Hello2";
$now[1]['si'] = "BumBum2";
$now[2]['name'] = "Hello3";
$now[2]['si'] = "BumBum3";
$now[3]['name'] = "Hello4";
$now[3]['si'] = "BumBum4";

echo json_encode($now)."<br>";

unset($now[0]);

echo json_encode($now);

输出:

[{"name":"Hello1","si":"BumBum1"},{"name":"Hello2","si":"BumBum2"},{"name":"Hello3","si":"BumBum3"},{"name":"Hello4","si":"BumBum4"}]

{"1":{"name":"Hello2","si":"BumBum2"},"2":{"name":"Hello3","si":"BumBum3"},"3":{"name":"Hello4","si":"BumBum4"}}

我的JSON文件变成了凌乱的代码。出现数字等 任何想法如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

您需要“重新索引”数组(使用array_values()函数)。

//unset..
unset($now[0]);

//reindex
$now = array_values($now);

//display as before
echo json_encode($now);