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);
}
}
$codein yourin_array()but where is it created?$code?$code?