I know that this has already been answered and accepted, but I wanted to add something that can help others who find it through a search.
I had the same problem when I was comparing a column array and keys in a PHP array, where in my post array I had an extra string value.
$_POST["bar"] = array("other"); $foo = array(array("name"=>"foobar")); foreach($foo as $key=>$data){ $foo[$key]["bar"]="0"; foreach($_POST["bar"] as $bar){ if($bar==$key){ $foo[$key]["bar"]="1"; } } }
From this, you might think that at the end of $foo[0]["bar"]
will be equal to "0"
, but it happened that when $key = int 0
was freely compared with $bar = string "other"
, the result was true
, to fix this, I am strictly comparing, but then it is necessary to convert the value of $key = int 0
to $key = string "0"
when the POST array was defined as array("other","0");
. It worked:
$_POST["bar"] = array("other"); $foo = array(array("name"=>"foobar")); foreach($foo as $key=>$data){ $foo[$key]["bar"]="0"; foreach($_POST["bar"] as $bar){ if($bar==="$key"){ $foo[$key]["bar"]="1"; } } }
The result was $foo[0]["bar"]="1"
if "0"
was in the array of POST columns and $foo[0]["bar"]="0"
if "0"
not in the array POST columns.
Remember that when comparing variables, variables cannot be compared, because, in your opinion, due to incorrect PHP variable settings.
source share