array_values() 函数返回一个包含给定数组中所有键值的数组,但不保留键名。
现有文件this.txt,内容如下:
{"this":[{"name":"a","url":"aa"},{"name":"b","url":"bb"},{"name":"c","url":"cc"}]}
代码:
<?php
$lu='this.txt';
$js= file_get_contents($lu);
$okjson=json_decode($js,true);
var_dump($okjson);
/*array(1) { ["this"]=> array(3) {
[0]=> array(2) { ["name"]=> string(1) "a" ["url"]=> string(2) "aa" }
[1]=> array(2) { ["name"]=> string(1) "b" ["url"]=> string(2) "bb" }
[2]=> array(2) { ["name"]=> string(1) "c" ["url"]=> string(2) "cc" } } }
*/
unset($okjson["this"][1]);
var_dump($okjson);
/*
array(1) { ["this"]=> array(2) {
[0]=> array(2) { ["name"]=> string(1) "a" ["url"]=> string(2) "aa" }
[2]=> array(2) { ["name"]=> string(1) "c" ["url"]=> string(2) "cc" } } }
*/
$okjson['this'] = array_values($okjson['this']);
var_dump($okjson);
/*
array(1) { ["this"]=> array(2) {
[0]=> array(2) { ["name"]=> string(1) "a" ["url"]=> string(2) "aa" }
[1]=> array(2) { ["name"]=> string(1) "c" ["url"]=> string(2) "cc" } } }
*/
?>