I have this PHP:
$ec = $trimmed['emails'];//has a value of "email"
$dc = $trimmed['date'];//has a value of "timestamp"
$data = array("email","domain","timestamp");
$num = count($data);
for ($i=0; $i<$num; $i++){
if ($data[$i] == $ec) {
$ec = $i;
} else if ($data[$i] == $dc) {
$dc = $i;
}
}
When I run this PHP $ec ends up equaling the location of domain and not email. Also, it is clear that the first if thinks both “email” and “domain” are equivalent to “email” because when I echoed out the loop the first time it shows $ec as “email”. Any ideas why?
Because on the second iteration
$ec(which is now0) is compared with"domain", which evaluates to true, proven with a simplevar_dump( 0 == "domain");, which outputstrue.As William Van Rensselaer suggests below, you can either use
===, or check out the function array_search, which will accomplish exactly what you’re trying to do.Example: