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 ?

Suraj Roy
  • 51
  • 2
  • 12
  • `if (($jsonarr->like[$r]->username == 'abc') && ($jsonarr->like[$r]->password == 'abc'))` Possible this thing. – Virb Aug 31 '16 at 12:23
  • Both blocks are executed during the same loop through or just over the entire loop cycle? – Fata1Err0r Aug 31 '16 at 12:25
  • you are doing this in loop so – Rakesh Sojitra Aug 31 '16 at 12:25
  • noo.. it giving the same result.. :( – Suraj Roy Aug 31 '16 at 12:25
  • You have two inputs, and you are supposed to echo two lines. Are you saying 4 lines are being echoed? – Sundeep Aug 31 '16 at 12:25
  • i dont know how is this going ,, when it founds nothing no match then it displays "no match" twice , i think because there are two objects. so, 2 iteration. but when when it finds matching record ,, it displays first "No match" the "Match". i dont know whats going on there. mysterious loop !!! – Suraj Roy Aug 31 '16 at 12:28
  • just check please ... i will just take your 2 minutes time .. :) – Suraj Roy Aug 31 '16 at 12:29
  • main problem is ,, when there is matching record ,, it displays "no match" and then also "match". – Suraj Roy Aug 31 '16 at 12:30
  • Suraj, I ran your code on both phpfiddle and my own local php server, and the outcome was as is expected "match" then "no match". see my answer below, should help you, as it shows more detail in the output – Robert Wade Aug 31 '16 at 12:33

1 Answers1

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";
}
?>
Robert Wade
  • 4,918
  • 1
  • 16
  • 35
  • You're too quick for me. ^This is my thought as well. – Fata1Err0r Aug 31 '16 at 12:26
  • 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 Roy Aug 31 '16 at 12:35
  • 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. – Robert Wade Aug 31 '16 at 12:49
  • 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 – Suraj Roy Aug 31 '16 at 12:58
  • 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. – Robert Wade Aug 31 '16 at 13:11
  • on the "record found block" can you delete those records from that original array. that is my actual task. i have to delete that position from that array in which i have found that match. – Suraj Roy Sep 01 '16 at 06:47
  • you would typically use `unset` to handle deleting your array item. There's a good writeup on how to use that here: http://stackoverflow.com/questions/369602/delete-an-element-from-an-array – Robert Wade Sep 01 '16 at 10:27