1

I want to check if an array key exists within an array.

I need to check up to 10 array keys.

When using array_key_exists I have to check each array key individually like so...

if (array_key_exists('key', $arr) and array_key_exists('key', $arr)) {}

I need to run that array key check up to ten times using and to append each array key check.

I searched online for a way to check multiple array keys at once to consolidate and keep neet this code. Then I found that array_key_exists is slower in performance than isset and empty.

Ref: How to check if multiple array keys exists

So my question(s) are:

  1. Is speed an issue at this point when using array_key_exists to check for 10 keys within an array in one operation/function?

  2. Is there a better way to do this?

array_key_exists is the only method I found that won't leave an undefined index error if that key doesn't exist.

Below is my code where I started using array_key_exists and the rest of the code not using array_key_exists is how I normally have this function routine.

// @Note
//  Let's discover how many indexes we need to apply to our this method variable

// If our index variable $key => $index[0] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

if (!isset($index[0])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method;

}

// If our index variable $key => $index[1] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[1])) {

    // ---
    // HERE I AM STARTING TO WRAP MY VARIABLE TO BE RETURNED IN ARRAY_KEY_EXISTS
    // ---

    // If our index array key exists within our this method array

    if (array_key_exists($index[0], $this_method)) {

        // Set our this method type and index count variable

        $this_method_type_and_index_count = $this_method [$index[0]];

    }

}

// If our index variable $key => $index[2] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[2])) {

    // ---
    // HERE I AM STARTING TO WRAP MY VARIABLE TO BE RETURNED IN ARRAY_KEY_EXISTS
    // ---

    // If our index array key exists within our this method array

    if (array_key_exists($index[0], $this_method) and array_key_exists($index[0], $this_method)) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]];

}

// ---
// BELOW IS HOW I HAVE THIS ROUTINE PRIOR TO ARRAY_KEY_EXISTS
// ---

// If our index variable $key => $index[3] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[3])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]];

}

// If our index variable $key => $index[4] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[4])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]];

}

// If our index variable $key => $index[5] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[5])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]];

}

// If our index variable $key => $index[6] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[6])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]] [$index[5]];

}

// If our index variable $key => $index[7] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[7])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]] [$index[5]] [$index[6]];

}

// If our index variable $key => $index[8] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[8])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]] [$index[5]] [$index[6]] [$index[7]];

}

// If our index variable $key => $index[9] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[9])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]] [$index[5]] [$index[6]] [$index[7]] [$index[8]];

}

// If our index variable $key => $index[10] is not set
// @Note
//  This means we have no further indexes to look for so let's stop now and set our this option variable

elseif (!isset($index[10])) {

    // Set our this method type and index count variable

    $this_method_type_and_index_count = $this_method [$index[0]] [$index[1]] [$index[2]] [$index[3]] [$index[4]] [$index[5]] [$index[6]] [$index[7]] [$index[8]] [$index[9]];

}

// Let's return our method type and index count variable

return $this_method_type_and_index_count;
4
  • 2
    array_intersect_key() Commented Feb 7, 2017 at 15:05
  • your code seems really strange and repetitive, you should try and clean it a little, maybe use camelCase (using_the_under_scroll_is_so_old_and_annoying). Commented Feb 7, 2017 at 15:18
  • also what is $this_method ? is it an a multi-array structure that has 7 subarrays ? Commented Feb 7, 2017 at 15:21
  • Thanks about array_interect. I will check it out. Thank you. About the underscores, I will only simplify it when I see fit, which for now I don't see it applying. I'm writing my code for devs as well and when you read my code it sounds self-explanatory, read out loud. I want to stay along that line for now. This is part of an API for devs I am working on and I plan on them reading the code and I don't want to leave room for questions. But it will always be considered to simplify as time goes on and I can see fit. Commented Feb 7, 2017 at 15:56

1 Answer 1

0

Consolidate all of that repetitious, script-bloat condition block into a simple, breakable loop. Return as much of the source array data as the array of keys qualifies for.

Code: (Demo)

$this_method = ['foo' => ['bar' => ['bard' => ['food' => 'barf']]]];

$indexes = ['foo', 'bar', 'bard'];

function getBySuccessiveKeys(array $haystack, array $keys)
{
    foreach ($keys as $key) {
        if (!key_exists($key, $haystack)) {
            break;
        }
        $haystack = $haystack[$key];
    }
    return $haystack; 
}
var_export(getBySuccessiveKeys($this_method, $indexes));

Output:

array (
  'food' => 'barf',
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.