前言
删掉空的数组json数据.
代码(cyg.php)
<?php //删除数组中代表空的数组. /*$entry =[ 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '' ]; foreach($entry as $k=>$val) { if(!$val) { unset($entry[$k]); } } print_r($entry);*/ echo "以下是json数据的操作"; //删除json中空的json $json_string= '[{"id":"1","name":"-1","age":"27","subject":false},{"id":"2","name":"liwen","age":null,"subject":""}]'; $data = json_decode($json_string,true);//把json转换成数组 foreach($data as $k=>$val)//遍历数组 { // print_r($val);exit(); foreach($val as $kk=>$v)//拿到每一个$val转化成的数组 { if(!$data[$k][$kk])//获取每一个元素,如果是空,就删掉 { unset($data[$k][$kk]); } } //$data['0']["name"]="aas"; //$data[$k][$val]="666"; $json_strings = json_encode($data);//在转化成json, } var_dump($json_strings);
json删掉空的效果:
删掉空的数组效果