如何在php中取消数组中的值?

时间:2014-06-30 12:19:42

标签: php arrays json unset

我的json数组看起来像这样

{
"id":"1507593152797237",
"handle":"naveen",
"firstName":"bos",
"lastName":"Hello",
"email":"testnaveen.bos@gmail.com",
"pic":null,
"following":[
"123"
],
"followers":[
],
"groups":[
],
"blocking":[
],
"blockers":[
],
"postUnfollowing":[
],
"followingGroups":[
],
"likes":[
]
},
{
"id":"1234",
"handle":"naveesn.bos",
"firstName":"naveenbos",
"lastName":"boss",
"email":"naveen.boss@gmail.com",
"pic":null,
"following":[
"123"
],
"followers":[
],
"groups":[
11
],
"blocking":[
],
"blockers":[
],
"postUnfollowing":[
],
"followingGroups":[
],
"likes":[
],
}

我想从此数组中删除不需要的项目,例如followGroups [],blockers [],阻止[],

  

$ response = array();

    foreach ($user["followers"] as $follower) {
        $response[] = $this->mongologue->user('find', $follower);
    }
    echo json_encode($response);

我需要从$ response中取消不需要的东西,意味着删除以下组[],阻止[],阻止[],请帮助。

3 个答案:

答案 0 :(得分:2)

正如我在评论中提到的那样实施....让我知道它是否对你有用!!

foreach ($user["followers"] as $follower) {
        $response[] = $this->mongologue->user('find', $follower);
    }

$unnecessary = array('followingGroups','blockers','blocking');
foreach ($response as $key1=>$res){
    foreach($res as $key => $new_res){
        if(in_array($key,$unnecessary))  
            unset($response[$key1][$key]);
    }
}
    echo json_encode($response);

答案 1 :(得分:0)

function findAndRemove(array, property, value) {
 $.each(array, function(index, result) {
  if(result[property] == value) {
      //Remove from array
      array.splice(index, 1);
  }    
 });
}

//检查followGroups.result是否有属性为'id'的对象,其值为'any value' //然后删除它; p findAndRemove(followGroups.result,'id','无论什么值');

答案 2 :(得分:-1)

您可以使用未设置的功能。

unset($response['blockers']);

使用此功能删除'拦截器'来自$ response array。