0

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;
  }
}
Picoral
  • 199
  • 1
  • 2
  • 13
  • 1
    Could you please add some minimal data to your question; examples of possible values and the data you're searching for? – Phil Mar 24 '19 at 02:18
  • That being said, this looks like a scoping issue. `$states_json` is not declared in your function – Phil Mar 24 '19 at 02:19
  • Yeah, this is the problem. When using `gettype($states_json)` within the function it returns `NULL`. How can I fix this? I tried adding a new param and changing `$states_json` for this param in the function, then calling the function with `$states_json` as this new param since `$states_json` is working fine outise the function, but this did not work also. The most strange part is that I used `$states_json` within others functions and I had no problems doing so. About the data, sorry for that. I'm calling this function with `validateInstCity("RS", "Porto Alegre")`, so it should return true... – Picoral Mar 24 '19 at 02:57
  • I found how to fix it seeing this question https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and - I had to include the file within all the functions that need `$states_json`. It slows down a litte but at least it's fixed. – Picoral Mar 24 '19 at 03:12
  • 1
    Solution to include only once: pass it as a parameter. It was not working before because I was calling the function from another function without including the file within that function. – Picoral Mar 24 '19 at 03:19

0 Answers0