如何使用PHP删除特定的JSON元素?

时间:2015-03-05 07:48:16

标签: php json

请帮助我解决问题,如何使用PHP删除特定的JSON元素。为了清楚的想法,请看下面的例子。

$print = '{"Table":[
{"Column":2,"Length":1,"Number":"A4","Row":12}, 
{"Column":2,"Length":1,"Number":"A3","Row":11},
{"Column":2,"Length":1,"Number":"A2","Row":9},
{"Column":2,"Length":1,"Number":"A1","Row":8},  
{"Column":3,"Length":1,"Number":"B4","Row":12},
{"Column":3,"Length":1,"Number":"B3","Row":11},
{"Column":3,"Length":1,"Number":"B2","Row":9},
{"Column":3,"Length":1,"Number":"B1","Row":8},
{"Column":4,"Length":1,"Number":"C4","Row":12},
{"Column":4,"Length":1,"Number":"C3","Row":11},  
{"Column":4,"Length":1,"Number":"C2","Row":9},
{"Column":4,"Length":1,"Number":"C1","Row":8}],"Times":[{"Address":null,"ContactNumbers":null,"ContactPersons":null,"PointId":"90","Landmark":"Kukatpally(Main Bus),Kukatpally, ,04024600350 8121971343 9533931290 9032005712","Location":"Kukatpally(Main Bus),Kukatpally, ,04024600350 8121971343 9533931290 9032005712","Name":null,"Time":"1195"}],"ResponseStatus":200,"Message":null}';

从我想删除的上述JSON数据" Times"我希望获得低于JSON数据的元素。

 $print = '{"Table":[
    {"Column":2,"Length":1,"Number":"A4","Row":12}, 
    {"Column":2,"Length":1,"Number":"A3","Row":11},
    {"Column":2,"Length":1,"Number":"A2","Row":9},
    {"Column":2,"Length":1,"Number":"A1","Row":8},  
    {"Column":3,"Length":1,"Number":"B4","Row":12},
    {"Column":3,"Length":1,"Number":"B3","Row":11},
    {"Column":3,"Length":1,"Number":"B2","Row":9},
    {"Column":3,"Length":1,"Number":"B1","Row":8},
    {"Column":4,"Length":1,"Number":"C4","Row":12},
    {"Column":4,"Length":1,"Number":"C3","Row":11},  
    {"Column":4,"Length":1,"Number":"C2","Row":9},
    {"Column":4,"Length":1,"Number":"C1","Row":8}],"Times":null,"ResponseStatus":200,"Message":null}';

请帮我解决上述问题。我提前谢谢你。

1 个答案:

答案 0 :(得分:1)

这应该这样做:

 $jsonObject = json_decode($print); //converting the json to object
 unset($jsonObject->Times); // removing 'Times'
 $jsonEncode = json_encode($jsonObject); //converting back to json string

尝试并测试过。

在:

{"Table":[ {"Column":2,"Length":1,"Number":"A4","Row":12}, {"Column":2,"Length":1,"Number":"A3","Row":11}, {"Column":2,"Length":1,"Number":"A2","Row":9}, {"Column":2,"Length":1,"Number":"A1","Row":8}, {"Column":3,"Length":1,"Number":"B4","Row":12}, {"Column":3,"Length":1,"Number":"B3","Row":11}, {"Column":3,"Length":1,"Number":"B2","Row":9}, {"Column":3,"Length":1,"Number":"B1","Row":8}, {"Column":4,"Length":1,"Number":"C4","Row":12}, {"Column":4,"Length":1,"Number":"C3","Row":11}, {"Column":4,"Length":1,"Number":"C2","Row":9}, {"Column":4,"Length":1,"Number":"C1","Row":8}],"Times":[{"Address":null,"ContactNumbers":null,"ContactPersons":null,"PointId":"90","Landmark":"Kukatpally(Main Bus),Kukatpally, ,04024600350 8121971343 9533931290 9032005712","Location":"Kukatpally(Main Bus),Kukatpally, ,04024600350 8121971343 9533931290 9032005712","Name":null,"Time":"1195"}],"ResponseStatus":200,"Message":null}

后:

{"Table":[{"Column":2,"Length":1,"Number":"A4","Row":12},{"Column":2,"Length":1,"Number":"A3","Row":11},{"Column":2,"Length":1,"Number":"A2","Row":9},{"Column":2,"Length":1,"Number":"A1","Row":8},{"Column":3,"Length":1,"Number":"B4","Row":12},{"Column":3,"Length":1,"Number":"B3","Row":11},{"Column":3,"Length":1,"Number":"B2","Row":9},{"Column":3,"Length":1,"Number":"B1","Row":8},{"Column":4,"Length":1,"Number":"C4","Row":12},{"Column":4,"Length":1,"Number":"C3","Row":11},{"Column":4,"Length":1,"Number":"C2","Row":9},{"Column":4,"Length":1,"Number":"C1","Row":8}],"ResponseStatus":200,"Message":null}

json_decode

unset

json_encode