无法检查值是否存在于数组中

时间:2018-10-17 17:52:14

标签: php arrays

我有一个保存变量的数组

  <?php
include_once '../Includes/Secure.php';
include_once '../Includes/ConnectionInfo.php';

/*Acquiring the security class*/
$mSecure = new Includes\Secure;
$mConnectionInfo = new Includes\ConnectionInfo();
$mConnectionInfo->GetConnection();

$email ="sule@gmail.com";

if ($mConnectionInfo->conn){
    echo "is connected <br/>";

    $stmt2 = $mConnectionInfo->conn->prepare('SELECT email, secret_key, secret_iv FROM users');

    $work2 = $stmt2->execute();
    $returnedvalue = array();
    if ($work2){
        while($row = $stmt2->fetch(PDO::FETCH_ASSOC)){

            $secret_key = $row['secret_key'];
            $secret_iv = $row['secret_iv'];

            $secret_key = $mSecure->my_simple_crypt_key($row['secret_key'],'d','sha384');//encrypt with sha384

            $secret_iv = $mSecure->my_simple_crypt_key($row['secret_iv'],'d','sha384');//encrypt with sha384


            $decryptedemail = $mSecure->my_simple_crypt($row['email'],'d','sha384',$secret_key,$secret_iv);//encrypt with sha384

            $value = ["Email" => $decryptedemail];
            array_push($returnedvalue, $value);
        }

        echo json_encode($returnedvalue);
        echo "<br/>";
        echo $email;

        if(in_array($email,$returnedvalue,TRUE)){
            echo "<br/> value exists";
        }
        else{
            echo "<br/> value doesnt exists<br/>";
        }
    }
}
?>

下面是输出

    is connected 
[{"Email":"alsongdunstan2@gmail.com"},{"Email":"sule@gmail.com"}]
sule@gmail.com
value doesnt exists

它表明sule@gmail.com在数组中,但是当我检查它是否存在时,它表明该值不存在。 需要一些有关如何检查数组$ returnedvalue中是否存在sule@gmail.com的帮助

1 个答案:

答案 0 :(得分:0)

您需要更改此部分...

        $value = ["Email" => $decryptedemail];
        array_push($returnedvalue, $value);

对此...

        array_push($returnedvalue, $decryptedemail);

基本数组结构的形式为:

$arr = array( value, value, value );

但是,您正在创建一个多维数组(请参见下文),并且无法通过in_array()函数进行“搜索”。

$arr = array ( array( key => value ), array( key => value ), array( key => value ) );