0

Strange PHP in_array issue tripping me. Not a PHP expert, but I'm a bit exhausted, and can't seem to catch the syntax or property I might be erring on.

This is what I'm trying to do:

  • Get a CSV file, turn it into an associative array
  • Look for a code in the array. It is a dynamic variable I get from a simple form submission.
  • Send a "Message" response to if the code exists, else send an "Error" response

To my understanding, the code below should've worked, but strangely enough, the moment I add the "else", the response always returns "error" (like, it never enters the if, or it's getting reset for every iteration to error).

If I remove the "else" condition inside for loop, it's working as expected, but I need to catch the error. Also tried strict comparison, no dice!

Any help is appreciated! Thank you once again.

$csv = array_map('str_getcsv', file($file,FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}
for ($j=0; $j<count($csv); $j++) {
    if (in_array($code, $csv[$j])) {
      $output = json_encode(array('type'=>'message', 'text' => 'Found'));
      die($output);
    } 
    else {
      $output = json_encode(array('type'=>'error', 'text' => 'Not Found'));
      die($output);
    }
}
3
  • 3
    You reference $code in your in_array() but where is it created? Commented Aug 3, 2018 at 4:58
  • 2
    where is the value of $code? Commented Aug 3, 2018 at 5:00
  • 2
    what is the value of $code? Commented Aug 3, 2018 at 5:00

3 Answers 3

1

Since you have a die() both in the if and the else-block, your code will only test the first iteration and then stop executing. That's why it works when you omit the else.

Change it to:

for ($j=0; $j<count($csv); $j++) {
    if (in_array($code, $csv[$j])) {
        $output = json_encode(array('type'=>'message', 'text' => 'Found'));
        die($output);
    } 
}

$output = json_encode(array('type'=>'error', 'text' => 'Not Found'));
die($output);

Since you have a die() in the if-block, any code after the loop will only be executed if the expression in the if-block never evaluates as true.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh &*$%, that was it. Thank you Magnus. I knew it was something basic. Sorry about the noob moment.
@Deep - No worries. We've all been there. :-) (and still are, from time to time).
1

You should use array_search to find by value in an associative array instead of using in_array like:

if (array_search($code, $csv[$j]))

4 Comments

Thank you for the response @Lovepreet. That wasn't my issue however. The condition was being met, but it was the iteration issue as Magnus points out. However, I'm curious - is there a specific documentation, or speed-test which justifies using array_search over in_array? From what I've read here stackoverflow.com/questions/4518404/… - in_array() seems faster. Thanks again :)
@Deep - Those two functions does different things. in_array() is simply checking if a value exists and returns a boolean, while array_search() will return the corresponding key, if the value is found. In your case, in_array() makes more sense.
When using array_search(), you should always check it with: array_search($value, $array) !== false since it can return 0 if the value is found and it has the key 0. If you do as the above, you can get a false negative.
Hey, yes! I knew about that. I was just curious as to how/ why it was suggested to use array_search here. As you mention, I was just looking for an "exists" check and send a response, so opted for the former. :)
0

You did not defined $code variable in your code and also you can do in code is

if(!empty($code)&& in_array($code,$csv[$j])){

}

you need to check $code variable empty or not!

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.