Home > Blockchain >  how to use the in_array function, that is storing a variable from an input field
how to use the in_array function, that is storing a variable from an input field

Time:09-27

I am trying to check if a value already exists in an array with the in_array function but it is not working. When i assign the $companyalis variable to a string, lets say "Bay" it works. Please how can i solve this issue. Note: the $alias contains a value from an input field.

 $companyalis = strtoupper($alias);
    if (is_array($responses)) {
        $data = [];
        foreach ($responses as $val) {
            $data[] = $val['alias'];
        }

        if (in_array($companyalis, $data)) {
            echo "Alias is already defined";
        }else {
            echo "Alias does not exist";
        }
    }
}

CodePudding user response:

$aliasList[] = "Other";
$aliasList[] = "manY";
$aliasList[] = "someTime";

$test = "Many";

if(in_array(strtolower($test),array_map('strtolower',$aliasList)))  echo "Find";
else echo "No Find";

I think this code can help you if I understood your question correctly.

CodePudding user response:

If I understood your question correctly

$companyalis = strtoupper($alias);
if (is_array($responses)) {
      $data = [];
      foreach ($responses as $val) {
            $data[] = $val['alias'];
      }

      if (in_array($companyalis, array_values($data))) {
            echo "Alias is already defined";
      } else {
            echo "Alias does not exist";
      }
}
  •  Tags:  
  • php
  • Related