I am processing a CSV authored by a remote system. I don’t want to assume they won’t change what fields they provide or their order so I am parsing a header row for the indices in the CSV of fields and mapping these into an array I can use for lookups and updates.
Thing is the way I am doing it tests false for index zero:
foreach ($field_map_array as $local_field => $silverpop_field) {
if ($idx = array_search($silverpop_field, $fields)) {
$local_to_sp[$local_table][$local_field] = $idx;
} // index found
}
What’s a better way to do that?
array_searchreturns an integer if it finds the needle, andFALSEotherwise, so test it explicitly using the!==/ not identical operator:Here’s how the manual describes the difference between
!=(not equal) and!==(not identical) (emphasis added):The problem with your code is that
if(0)is interpreted asif(0 != FALSE). Type juggling lets it cast FALSE to the integer 0, and0 != 0is obviously false. With!==type juggling doesn’t occur, so0 !== FALSEis true (since they’re not the same type).I’d recommend using
===and!==whenever possible, since they are more predictable, and predictable code tends to be less buggy and easier to work with.