I was trying to find a value in a multi-array variable. It took too much time to find where was my bug...
Try this code:
$aa = array("nombre" => "HOLA", "v" => 0);
$bb = array("nombre" => "HOLB", "v" => 0);
$cc = array("nombre" => "HOLC", "v" => 0);
$dd = array($aa,$bb,$cc);
if (in_array("HOLA",array_column($dd,"nombre")))
echo "in_array = yes";
else
echo "in_array = no";
echo "<br>";
if (array_search("HOLA",array_column($dd,"nombre")))
echo "array_search = yes";
else
echo "array_search = no";
the answer I get is:
in_array = yes
array_search = no
It is this a supposed behavior?
Yes. The documentation for array_search states the following:
Warning: This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
The value you are getting is 0 , which is failing your if
, but is the correct answer.
array_sarch
will return the index at which it finds the result, so in your case 'HOLA'
is at the 0
index which makes the condition fail. You should check it like this
if (array_search("HOLA",array_column($dd,"nombre")) !== false)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.