我在php中有数组并在json.Now中转换它 我有这个json格式的代码
{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
} 以json格式 但我想删除索引2,3,4,5表单代码并希望数据
{
"12:40 to 13:0",
"13:0 to 13:20",
"13:20 to 13:40",
"13:40 to 14:0",
"14:0 to 14:20",
"14:20 to 14:40",
"14:40 to 15:0",
"15:0 to 15:20",
"15:20 to 15:40",
"15:40 to 16:0",
"16:0 to 16:20",
"16:20 to 16:40",
"16:40 to 17:0",
"17:0 to 17:20",
"17:20 to 17:40",
"17:40 to 18:0"
}
任何帮助:)
答案 0 :(得分:2)
答案 1 :(得分:1)
是的,您可以使用PHP array_values
删除密钥并保留唯一值只需json_decode
,然后使用array_values
。
默认情况下,数组索引是数字。
如果您生成数组,例如$arr = array('PHP', 'JAVA')
,你会得到
这是数字数组
$arr = (0 => 'PHP', '1' => 'JAVA')
现在,如果我们创建一个新的关联数组。
$arr = ('scripting' => 'PHP', 'programming' => 'JAVA')
因此,array_values将数组从关联数组转换为数值数组。
答案 2 :(得分:0)
尝试 -
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
$data = (array)json_decode($json); //your json data
$data_values = array_values($data);//if you want array
$newStr = "{".implode(',', $data_values)."}"; // if you want string
$newJson = json_encode($data_values); //if you want json
答案 3 :(得分:0)
我希望你想从JSON中删除索引,这是方法:
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
$json = json_encode(array_values(json_decode($json,true)));
答案 4 :(得分:0)
试试这个
<?php
$json = '{
"2": "12:40 to 13:0",
"3": "13:0 to 13:20",
"4": "13:20 to 13:40",
"5": "13:40 to 14:0",
"6": "14:0 to 14:20",
"7": "14:20 to 14:40",
"8": "14:40 to 15:0",
"9": "15:0 to 15:20",
"10": "15:20 to 15:40",
"11": "15:40 to 16:0",
"12": "16:0 to 16:20",
"13": "16:20 to 16:40",
"14": "16:40 to 17:0",
"15": "17:0 to 17:20",
"16": "17:20 to 17:40",
"17": "17:40 to 18:0"
}';
echo json_encode(array_values(json_decode($json, true)));
?>
{"12:40 to 13:0","13:0 to 13:20",.....}
当密钥不在JOSN中时,这不是有效的json格式。
正确的格式为["12:40 to 13:0","13:0 to 13:20","13:20 to 13:40".....]