Удалить из json, используя php

мой текущий код json:

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

Я хочу удалить это:

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

из кода с использованием php. Я попытался удалить, расшифровав json до массива, но не могу сделать это. любое решение?

 $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); не $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); 
 <?php $JSON = '{"Results":[' . '{"username":"test","password":"test"},' . '{"username":"test","password":"test"},' . '{"username":"google","password":"test"},' . '{"username":"yahoo","password":"test"},' . '{"username":"hotmail","password":"test"}' . ']}'; // use json_decode to parse the JSON data in to a PHP object $jsonInPHP = json_decode($JSON); // now iterate over the results and remove the one that's google $results = count($jsonInPHP->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); не <?php $JSON = '{"Results":[' . '{"username":"test","password":"test"},' . '{"username":"test","password":"test"},' . '{"username":"google","password":"test"},' . '{"username":"yahoo","password":"test"},' . '{"username":"hotmail","password":"test"}' . ']}'; // use json_decode to parse the JSON data in to a PHP object $jsonInPHP = json_decode($JSON); // now iterate over the results and remove the one that's google $results = count($jsonInPHP->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); 

Было бы так, как я к нему подхожу. Мне нравится избегать foreach(...){} операторов, когда мне нужно изменить сам массив. Вышеприведенный код, кстати, оставляет вас:

 { "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'])]); 

Чтобы сделать это без foreach, но при условии, что вы знаете точные значения, которые хотите удалить

Старый вопрос, форматирование вашего JSON по-разному поможет много. Каждая запись результата должна иметь уникальный ключ для ее идентификации. Это упрощает процесс удаления или обновления этого результата. Нет причин перебирать весь JSON таким образом.

Код будет выглядеть так:

 <?php $jsonString = '{"Results":{' .'{"username1":{"username":"google","password":"test1"}}' .'{"username2":{"username":"yahoo","password":"test2"}}' .'{"username3":{"username":"msonline","password":"test3"}}' . '}}'; $jsonInPHP = json_decode($jsonString); $password = $jsonInPHP["username1"]["pasword"];//Returns test1 $username = $jsonInPHP["username1"]["username"];//Returns google 

?>