Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
567 views
in Technique[技术] by (71.8m points)

php - 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 IncludesSecure;
$mConnectionInfo = new IncludesConnectionInfo();
$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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ) );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...