简体   繁体   中英

PHP - Check if Array exists in Multidimensional Array with an Exact Match of Pair

I need to check if an exact pair of two values exists in a multidimensional array together.

I have an array like this:

Array
(
[code] => 200
[response] => Success
[0] => Array
    (
        [email] => example123@sample.com
        [status] => Approved: Printed & Cleared
    )

[1] => Array
    (
        [email] => xxexample123@sample.com
        [status] => Pending
    )

[2] => Array
    (
        [email] => example1345@sample.com
        [status] => Approved
    )

[3] => Array
    (
        [email] => example1235@sample.com
        [status] => Approved: Printed & Cleared
    )
)

Then I have an array that looks like this:

Array
    (
        [email] => xxexample123@sample.com
        [status] => Pending
    )

I need to check if that exact pair exists in the multidimensional array. Not just that the status and email appear seperate from each other.

You can use array_search() in the same manner if you need to get the key, but at its simplest (assuming $array1 only has the keys and values that are being search for and in the same order):

$array2 = array('email'  => 'example123@sample.com',
                'status' => 'Pending');

if(in_array($array2, $array1)) {
    //yes
} else {
    //no
}

See the Demo showing found and not found.

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