Home > Software design >  in_array() not working properly using $_POST
in_array() not working properly using $_POST

Time:12-27

I'm trying to let the user select multiple countries and then check if the countries he selected are in the array of allowed countries using PHP I used javascript for multiple options , this is why it is not found in the tags. Otherwise for testing you can put some custom options between the tags. (country codes in this case) HTML:

  <form method="POST" action="">
                    <select name="countries[]"  multiple></select>                        
                    
                    <button type="submit">Submit</button>
                    </form>

PHP:

 <?php 
if(isset($_POST['countrylist'])) {
    $arr = array("FR","GF","PF","TF","GE","DE","GI","GR","GL","HK","IS","IN","ID","IE","IL","IT","JP","JO","KZ","KR","LV","LB","LI","LT","LU","MK","MX","MD","MC","MN","MS","MA","NP","NL","NZ","NO","PK"); //These are allowed country codes which are sent by the user the same way as above using POST request. The sent array looks fine as you can see in the var_dump result below.
    $array2 = (array) $_POST['countries'];
    
    $array2=  array_filter($array2);
if(!in_array($array2, $arr)) {
echo var_dump($array2);
}
} 
?>

The in_array() function doesn't seem to work , my var_dump result looks something like this:

array(1) { [0]=> string(2) "FR" }  //If I selected France country for example.

CodePudding user response:

$arr is an array of strings. $array2 is an array of strings (just one string in this case).

You are using $array2 as your needle, so you are asking "Does this array appear in this set of strings", which is doesn't, because the array isn't a string.

You would need to do something more like in_array($array2[0], $arr).

That, of course, misses the point since you want to accept multiple values, but it is a place to start.

You need to loop over $array2 and test each value in turn until you get to the end or hit a failure state. You might also look at using array_filter again, this time passing a callback.

CodePudding user response:

in_array() works just fine but you call with wrong arguments. The first argument that it expects is a value to search in the second argument (that must be an array).

Since you have an array of values that you want to search you can either try to search each value at a time using in_array() or you can use array_intersect() to find the values that are present in both arrays.

Your choice will be influenced by the way you decide to handle the unexpected values in the input.

Example using in_array():

$knownValues = ['FR', 'GF', 'PF', 'TF'];

if (! is_array($_POST['countries'])) {
  // The input does not look valid; there is nothing to process
  // Handle this in the most appropriate way for your application and stop here
  // return/exit/throw/whatever
}

$valid = [];
foreach ($_POST['countries'] as $countryId) {
  if (! in_array($countryId, $knownValues)) {
    // $countryId is not a valid/known country code
    // Do something with it (report it, ignore it etc.)
    
    continue;
  }

  $valid[] = $countryId;
}

// Here $valid contains the input values that are present in $knownValues

Example using array_intersect():

$knownValues = ['FR', 'GF', 'PF', 'TF'];

if (! is_array($_POST['countries'])) {
  // The input does not look valid; there is nothing to process
  // Handle this in the most appropriate way for your application and stop here
  // return/exit/throw/whatever
}

$valid = array_intersect($_POST['countries'], $knownValues);
$invalid = array_diff($_POST['countries'], $knownValues);

// The known/valid and unknown/invalid values have been separated
// Do whatever you want with them.
  •  Tags:  
  • php
  • Related