Eliminar de json con php

mi código json actual:

{"Results":[{"username":"test","password":"test"},{"username":"test","password":"test"},{"username":"google","password":"test"},{"username":"yahoo","password":"test"},{"username":"hotmail","password":"test"}]} 

Quiero eliminar esto:

 {"username":"google","password":"test"} 

del código usando php. Intenté eliminar decodificando json en array, pero no puedo hacerlo. alguna solución ?

 $json_obj = json_decode($json_string); $unset_queue = array(); foreach ( $json_obj->Results as $i => $item ) { if ($item->username == "google") { $unset_queue[] = $i; } } foreach ( $unset_queue as $index ) { unset($json_obj->Results[$index]); } // rebase the array $json_obj->Results = array_values($json_obj->Results); $new_json_string = json_encode($json_obj); 
 Results); for ($r = 0; $r < $results; $r++){ // look for the entry we are trying to find if ($jsonInPHP->Results[$r]->username == 'google' && $jsonInPHP->Results[$r]->password == 'test'){ // remove the match unset($jsonInPHP->Results[$r]); // now we can either break out of the loop (only remove first match) // or you can use subtract one from $r ($r--;) and keep going and // find all possible matches--your decision. break; } } // now that we removed items the keys will be off. let's re-order the keys // so they're back in-line $jsonInPHP->Results = array_values($jsonInPHP->Results); // dump the new JSON data, less google's entry echo json_encode($jsonInPHP); 

Sería cómo lo aborde. Me gusta evitar las declaraciones foreach(...){} cuando necesito modificar la matriz en sí. El código anterior, por cierto, te deja con:

 { "Results":[ {"username":"test","password":"test"}, {"username":"test","password":"test"}, {"username":"yahoo","password":"test"}, {"username":"hotmail","password":"test"} ] } 
 $myArray=json_decode($theJSONstring); unset($myArray['Results'][2]); 
 $json = ' { "Results":[ {"username":"test","password":"test"}, {"username":"test","password":"test"}, {"username":"google","password":"test"}, {"username":"yahoo","password":"test"}, {"username":"hotmail","password":"test"} ] }'; $arr = json_decode($json, true); array_filter($arr, function($v) { return !($v['username'] == 'google' && $v['password'] == 'test'); }); $json = json_encode($arr); 
 $input='{"Results":[{"username":"test","password":"test"},{"username":"test","password":"test"},{"username":"google","password":"test"},{"username":"yahoo","password":"test"},{"username":"hotmail","password":"test"}]}'; $json = json_decode($input,true); $match = array('username'=>'google', 'password'=>'test'); unset($json['Results'][array_search($match,$json['Results'])]); 

Para hacerlo sin un foreach pero suponiendo que conoce los valores exactos que desea eliminar

Una vieja pregunta, formatear tu JSON de manera diferente ayudaría mucho. Cada entrada de resultado debe tener una clave única para identificarlo. Esto facilita la tarea de eliminar o actualizar ese resultado. No hay razón para iterar sobre JSON completo de esta manera.

El código se vería así

  

?>