简体   繁体   中英

php In _array not working correctly with this code

For some reason, I cannot get the in_array function to work properly with this code. I want to check to see if a specific number is in the array returned back from the database. So I passed it into the in_array function but it is still returning false even when I add or remove quotations around the number.

    <?php 

     include('connection.php');

     $sql = mysqli_query($conn,"SELECT sku FROM inventory");

     $arr = array();

     while($row = mysqli_fetch_array($sql)){
       $arr[] = $row;

       //echo $row['sku']. "<br>";
   }

     //echo $arr[0]['sku'];

        print_r($arr);

   if(in_array(12345678, $arr[0], TRUE)){
       echo "here";
    }else{
      echo "not here";
   }
  ?>

You are making the array to complicated for in_array() If you make it a single dimension array rather than a multi dimension array it will work.

<?php 

include('connection.php');

$sql = mysqli_query($conn,"SELECT sku FROM inventory");

$arr = [];

while($row = mysqli_fetch_array($sql)){
    $arr[] = $row['sku'];
}

print_r($arr);

if(in_array('12345678', $arr)){
    echo "here";
}else{
    echo "not here";
}
?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM