2014-06-30 120 views
-1

我有一個JSON陣列看起來像這樣如何從PHP中的數組中取消設置值?

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

我想從這個數組等followingGroups [],阻滯劑[],阻斷[],

$響應=陣列(除去不需要的項目);

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

我需要從$迴應取消設置不必要的東西,是指除去thsese事情followingGroups [],β受體阻滯劑[],阻止[3],請幫助。

+0

上面的json對象數組創建自json_encode($ response);? json_encode($ response) –

+0

@ShaunakShukla yes json_encode($ response) – Naveenbos

+0

然後你可以在json_encode($ response)之前使用[Suman Biswas](http://stackoverflow.com/a/24490075/2679536)的答案。但在循環中!或者,在添加到$ response []之前,您可以在當前的foreach循環中保留條件! –

回答

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); 
+0

Shaunak Shukla謝謝它正在工作。非常感謝。 – Naveenbos

-1

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

unset($response['blockers']); 

用於從$ response數組中刪除'blockers'。

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

//檢查followingGroups.result的對象與它的值是 '的任何值' //然後刪除它的 'id' 的屬性,P findAndRemove(followingGroups.result, 'ID','無論什麼價值「);

相關問題