0

my Current json code :

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

i want to remove this :

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

from the code using php. i tried deleting by decoding json to array but cant get it done. any solution ?

1
  • i tried doing this but it doesnt work. unset($decodedata['Results'](array_search("google",$decodedata['Results'])); Commented Aug 12, 2011 at 18:57

6 Answers 6

6
$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);
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for unsetting outside of the foreach, as to not destroy the iteration. Clever!
I'm not sure if OP wants it, but this version will include array keys in the result because the keys are no longer in series. e.g. note the 1 & 3 in the following: "1":{"username":"test","password":"test"},"3":{"username":"yahoo","password":"test"}
This could be simplified by forcing json_decode to return only an associative array json_decode($json, true). Inside a foreach doing unset on an array shouldn't matter because the foreach() iterates over a copy of the original array, so any changes to the original array won't modify the copy it is iterating over.
3
<?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);

Would be how I approach it. I like to avoid foreach(...){} statements when I need to modify the array itself. The above code, by the way, leaves you with:

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

Comments

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

2 Comments

i dont want to use [2] as i cant figure out the value
@codecute: I don't see that requirement in your question. I've now replaced that line in my code with array_filter, to only filter the entries where username is google and the password is test. Hope that helps
0
$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'])]);

To do it without a foreach but assuming you know the exact values you want to remove

Comments

0

Old question, formatting your JSON differently would help a lot. Each result entry should have a unique key to identify it. This makes it easy when needing to remove or update that result. No reason to iterate over entire JSON this way.

Code would look like this

<?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

?>

Comments

-1
$myArray=json_decode($theJSONstring);
unset($myArray['Results'][2]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.