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:
Is speed an issue at this point when using
array_key_exists
to check for 10 keys within an array in one operation/function?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;