0

i have a simple json array. in which i have two objects. now i am checking it for some specific values , if found do some thing , if not found do another thing. But i dont know why if condition runs both blocks. true and false both blocks are executed. please help.

$jsonstring = '{"like":[{"username":"abc","password":"abc"},{"username":"def","password":"def"}]}';
$jsonarr = json_decode($jsonstring);
$count = count($jsonarr->like);
for ($r = 0; $r < $count; $r++) 
{
    if ($jsonarr->like[$r]->username == 'abc' && $jsonarr->like[$r]->password == 'abc') 
    { 
         echo 'Match';
    }else
    {
        echo 'No Match';
    }
}

is it not like regular if condition ?

9
  • if (($jsonarr->like[$r]->username == 'abc') && ($jsonarr->like[$r]->password == 'abc')) Possible this thing. Commented Aug 31, 2016 at 12:23
  • Both blocks are executed during the same loop through or just over the entire loop cycle? Commented Aug 31, 2016 at 12:25
  • you are doing this in loop so Commented Aug 31, 2016 at 12:25
  • noo.. it giving the same result.. :( Commented Aug 31, 2016 at 12:25
  • You have two inputs, and you are supposed to echo two lines. Are you saying 4 lines are being echoed? Commented Aug 31, 2016 at 12:25

1 Answer 1

2

It's showing both blocks because you are looping thru the json array which contains two items. One matching your condition, the other not matching. Here is a simpler version that does not use a loop but just tests your value if in the array. Notice the 'true' flag in the json_decode as well.

<?php

$jsonstring = '{"like":[{"username":"abc","password":"abc"},{"username":"def","password":"def"}]}';
$jsonarr = json_decode($jsonstring, true);

$testArray = $jsonarr['like'];
$loginArray = array('username'=>'abc','password'=>'abc');

if (in_array($loginArray,$testArray)) {
    echo 'match found, do login';
} else {
    echo "no match, do something else";
}
?>
Sign up to request clarification or add additional context in comments.

7 Comments

You're too quick for me. ^This is my thought as well.
ok.. i got that ,, i was thinking this is checking like login system... if record found in array do login otherwise login failed.. something like this. but it is totally doing another way.. how can i check a record exist or not in a json array ?
Suraj, The loop you're using will work for that. You just need to put your login methods in the 'match' part of the condition (the 'if'), and do nothing in the 'no match' part of the condition (the 'else'). You honestly don't need the else part of your condition here, since it's going to do nothing. You would just use the if. Then it's going to loop thru your array and run whatever methods are in your 'if' when it finds a match.
thnxx.. but i just told u about login method as an example.. i simple need --- if record exist do one thing, like call a function & if no match found, call another function. @RobertWade
OK Suraj, I think I understand what you're after better now. I modified my answer above with a different piece of code that uses in_array() to test if your login info is actually in that array.
|

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.