Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 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 ="[email protected]";

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":"[email protected]"},{"Email":"[email protected]"}]
[email protected]
value doesnt exists

it shows that [email protected] 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 [email protected] exists in array $returnedvalue

See Question&Answers more detail:os

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...