Home > Software engineering >  Multidimensional array: check if key exists
Multidimensional array: check if key exists

Time:07-26

I got an array with some user date like this:

$users = array(

    "[email protected]"        => array(
    
        "Name"      =>  "Bill Gates",
        "Password"  =>  "12345",),

    "[email protected]" => array(
    
        "Name"      =>  "Jim Franklyn",
        "Password"  =>  "98765"),       

);

How can i easily check the following conditions:

a) does $_POST['email'] exist in the array (as a key!)? b) get the password

I know that in_array doesn't work with multi arrays, also it should not search the whole array incl. sub arrays for the email - only the "key".

Is the only way to "foreach"?

CodePudding user response:

You can use array_key_exists to check if the key exists in the array, and then get the value. So you have:

<?php
$users = array(

    "[email protected]"        => array(
    
        "Name"      =>  "Bill Gates",
        "Password"  =>  "12345",),

    "[email protected]" => array(
    
        "Name"      =>  "Jim Franklyn",
        "Password"  =>  "98765"),       

);

$key = "[email protected]";
if(array_key_exists($key, $users)) {
    echo "Password is: " . $users[$key]["Password"];
} else {
    echo "Not Found!";
}

CodePudding user response:

Try this, this will help you. First store post email in $email variable then pass it to array_key_exists(). If email exist in the $users array then further it will get password from it.

$email = $_POST['email'];  
if(array_key_exists($email, $users)){
   $password = $users[$email]["Password"];
}else{
   echo 'Email not found';
}

CodePudding user response:

Here is a bit more general way, that is using a while loop and array_key_exists.

/**
 * Search for a Key Combination in a Multidimensional Array
 * 
 * @param array $keysArray Your key combination you are looking for
 * @param array $currentArray Your array
 * @return bool
 */
function array_key_combination_exist(array $keysArray, array $currentArray) {
    $return = true; //default answer.
    //loop through keys
    while($currentKey = array_shift($keysArray)){
        //if key exist, replace $currentArray with the result, for searching then in the sub array.
        if(array_key_exists($currentKey, $currentArray)) { 
            $currentArray=$currentArray[$currentKey];
        } else {
            //if key doesn't exist, just set the anser to false and exit the while loop.
            $return = false;
            break;
        }
    }
    return $return;
}

$keys = ['[email protected]', 'Password'];
$users = [
    "[email protected]"        => [
        "Name"      =>  "Bill Gates",
        "Password"  =>  "12345"],

    "[email protected]" => [
        "Name"      =>  "Jim Franklyn",
        "Password"  =>  "98765"],
];
$result = array_key_combination_exist($keys, $users);

You just need to fill the $keysArray. Each entry in that stands for a level. So pay attention on the order. With array_key_exists it looks if the key exists, and from then on go directly to the sub array. In this way you don't need a Foreach for your array, just a while loop for your keys. And you can it with really much deeper nesting without much rewriting work for you.

  • Related