从JSON PHP中删除值
我是PHP新手,正在开发wordpress JSON api。我想删除JSON数组中的Key:value对。请帮忙从JSON PHP中删除值,php,json,json-api,wordpress-json-api,Php,Json,Json Api,Wordpress Json Api,我是PHP新手,正在开发wordpress JSON api。我想删除JSON数组中的Key:value对。请帮忙 { "status":"ok", "post":{ "id":23, "type":"post", "slug":"head-ache", "url":"http:\/\/xyz.com\/maruthuvam\/2015\/06\/17\/head-ache\/", "status":"publish", "title":"Head Ache"
{
"status":"ok",
"post":{
"id":23,
"type":"post",
"slug":"head-ache",
"url":"http:\/\/xyz.com\/maruthuvam\/2015\/06\/17\/head-ache\/",
"status":"publish",
"title":"Head Ache",
"title_plain":"Head Ache",
"content":"<p>content<\/p>\n<p> <\/p>\n",
"excerpt":"<p>content <\/p>\n",
"date":"2015-06-17 19:35:47",
"modified":"2015-06-18 07:35:39",
"categories":[
{
"id":1,
"slug":"head",
"title":"Head",
"description":"http:\/\/xyz.com\/maruthuvam\/wp-content\/uploads\/2015\/06\/universa_-male_head_3d_model_01.jpg",
"parent":0,
"post_count":3
}
],
"tags":[
],
"author":{
"id":1,
"slug":"admin",
"name":"admin",
"first_name":"",
"last_name":"",
"nickname":"admin",
"url":"",
"description":""
},
"comments":[
],
"attachments":[
],
"comment_count":0,
"comment_status":"closed",
"custom_fields":{
}
},
"next_url":"http:\/\/xyz.com\/maruthuvam\/2015\/06\/17\/head- lice\/"
}
}您需要将其解析为普通数组,然后删除所需内容并将其编码回json:
// parsing json
$arr = decode_json($yourJson);
// removing the value
unset($arr['post']['slug']);
// and back to json
$editedJson = json_encode($arr);
只需将其转换为PHP数组:
$jsonArray = json_decode($jsonString);
取下钥匙
unset($jsonArray['post']['slug']);
并转换回:
$newJson = json_encode($jsonArray);
$a=json\u解码($data,true);
未结算($a['post']['slug']);
未设置($a['next_url']);
$count=count($a['post']['categories']);
对于($i=0;$i<$count;$i++){
未结算($a['post']['categories'][$i]['post_count']);
}
echo json_编码($a);
将json转换为数组,删除键,然后再转换回json。可能重复此操作:此答案缺少教育性解释。
$newJson = json_encode($jsonArray);
$a= json_decode($data,true);
unset($a['post']['slug']);
unset($a['next_url']);
$count= count($a['post']['categories']);
for($i=0 ; $i < $count ; $i++){
unset($a['post']['categories'][$i]['post_count']);
}
echo json_encode($a);