简体   繁体   中英

How to check if a value exists in a array using PHP

I am trying to check if a user input value exists in a array list using PHP. Here is my code:

$chkvalue=$_POST['id'];

$sql=$dbh->prepare("my query hear");
$sql->execute();
$memers=$sql->fetch(PDO::FETCH_BOTH);

I got my list of values in array $memers . When I use var_dump($memers); I can see all the values in the array. Now, I want to check weather $chkvalue exists in the array $memers .

I tried the code below:

if (in_array($chkvalue ,$memers)) {
   echo "Value exists";
  } else {
    echo "Value doesn't exists";
    }

But, it is always showing Value doesn't exists .

For example: my array contains {23568,456982,123489,125895,154879,124648}

Now I want to check if 456982 exists in that array or not.

i tried FETCH_NUM

i got below result

array(5) { [0]=> string(6) "600258" [1]=> string(15) "A SURYANARAYANA" [2]=> string(6) "420575" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

array(5) { [0]=> string(6) "223511" [1]=> string(20) "A UMA MAHESWARA RAO" [2]=> string(6) "600258" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" }

 array(5) { [0]=> string(6) "907774" [1]=> string(19) "A UMA MAHESWARA RAO" [2]=> string(6) "223511" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

array(5) { [0]=> string(6) "688108" [1]=> string(13) "M BALA BALAJI" [2]=> string(6) "907774" [3]=> string(1) "A" [4]=> string(10) "2016-07-05" } 

Could be you issue is related to the facth you retrive the value using PDO::FETCH_BOTH so $nemers contain the result i several diffrente way

then try using

$memers=$sql->fetch(PDO::FETCH_NUM);

But if your result is an associative array you should us somthings like this

<?php
    $chkvalue=$_POST['id'];
    $sql=$dbh->prepare("SELECT * FROM ???"); // Put a table name where the ? is.
    $sql->execute();
    while($row = $sql->fetch(PDO::FETCH_ASSOC))
    {
        foreach ($$row as $key => $value) {
          if ( $value == $chkvalue ) {
             echo "Value exists";
          }

        }
    }
?>

i don't understand why people give negative voting. there is always a option to discuss. how ever i got my answer in_array dose not work with multidimensional array so below code works

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

and for array

if(in_array_r($chkvalue, $memers)){
        echo 'found';
        }else{
            echo 'Not Found';       
            }

I strongly recommend using $stmt->fetch(PDO::FETCH_ASSOC) instead of $stmt->fetch(PDO::FETCH_BOTH) . You could do what you're looking for either in php or in the actual command.

PHP:

<?php
$chkvalue=$_POST['id'];
$sql=$dbh->prepare("SELECT * FROM ???"); // Put a table name where the ? is.
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
    $lookup_value = '20485930';
    if($lookup_value === $row['column_name'])
    {
        // Here is a match
    }
}
?>

SQL with PDO

<?php 
$chkvalue=$_POST['id'];
$sql=$dbh->prepare("SELECT * FROM ??? WHERE ??? = :value"); // Put a table name 
$sql->bindValue(':value', $value_name, PDO::PARAM_STR);
// $value_name is a placeholder for the actual value to put here. 
$sql->execute();
if($sql->num_rows > 1)
{
    // You have multiple inserts for this value
}
else 
{
    // You have only one value.
}
?>

Hope this helps.

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