-1

I have a json array look like this

{
"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":[
],
}

I want to remove unwanted items from this array like followingGroups[],blockers[], blocking[],

$response =array();

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

i need to unset unwanted things from $response, means remove thsese things followingGroups[],blockers[], blocking[], please help.

Naveenbos
  • 2,532
  • 3
  • 34
  • 60
  • Is above json object array created from json_encode($response);? – Shaunak Shukla Jun 30 '14 at 12:30
  • @ShaunakShukla yes this json objet created from json_encode($response) – Naveenbos Jun 30 '14 at 13:19
  • Then you can use answer of [Suman Biswas](http://stackoverflow.com/a/24490075/2679536) before json_encode($response); but in the loop! Or you can keep condition in your current foreach loop before adding to $response[]! – Shaunak Shukla Jun 30 '14 at 13:43

3 Answers3

2

just implemented as I mentioned in comment.... Let me know if it is useful to you or not!!

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);
Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
0
function findAndRemove(array, property, value) {
 $.each(array, function(index, result) {
  if(result[property] == value) {
      //Remove from array
      array.splice(index, 1);
  }    
 });
}

//Checks followingGroups.result for an object with a property of 'id' whose value is 'whatever value' //Then removes it ;p findAndRemove(followingGroups.result, 'id', 'whatever value');

user3789242
  • 105
  • 2
  • 12
-1

You can use unset function.

unset($response['blockers']);

Use this for remove 'blockers' from $response array.

Suman Biswas
  • 853
  • 10
  • 19