I'm trying to check if a value is in a array but it's always returning false. I've tried to fix this in quite a few different ways but none worked.
I have this file which I used require_once '../path/';
to add it to my current script. It is a JSON that was converted to PHP nested arrays.
This is the function that is always returning false
. I did a lot of testing using echo
and everything looks fine with $states_json
and the array $cities
.
If anyone could help me with this situation I would be apprciated.
EDIT: I'm calling this function with validateInstCity("RS", "Porto Alegre")
so it was supposed to return true
. After some more testing, I found out that the problem is that $states_json
is NULL
within the function. The strange part is that I used it inside others functions before without any problems. As you may see on the file, when using validateInstCity("RS", "Porto Alegre")
$idx should be 22
and the function should return true
.
function validateInstCity($inst_province = null, $inst_city = null) {
if (empty($inst_province) ||
empty($inst_city)) {
}
$idx;
for ($i=0; $i < count($states_json); $i++) {
if ($states_json[$i]['sigla'] == $inst_province) {
$idx = $i;
break;
}
}
$cities= array();
for ($i=0; $i < count($states_json[$idx]['cidades']); $i++) {
array_push($cities, $states_json[$idx]['cidades'][$i]);
}
if (in_array($inst_city, $cities, false)) {
return true;
} else {
return false;
}
}