简体   繁体   中英

unable to check whether value exists in array

I have an array that holds variables

  <?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/>";
        }
    }
}
?>

below is the output

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

it shows that sule@gmail.com is in the array but when i check if it exists it shows that value doesnt exist. need some help on how to check whether sule@gmail.com exists in array $returnedvalue

You need to change this part...

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

to this...

        array_push($returnedvalue, $decryptedemail);

A basic array structure is in the form:

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

However, you are creating an array that is multi-dimensional (see below) and is not able to be "searched" via the in_array() function.

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

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