Current Location: Home> Latest Articles> How to Quickly Check if a Key Exists in an Array with key_exists() in PHP? A Detailed Guide to Basic Usage

How to Quickly Check if a Key Exists in an Array with key_exists() in PHP? A Detailed Guide to Basic Usage

gitbox 2025-09-19

1. Introduction to the key_exists() Function

key_exists() is a built-in PHP function used to check whether a given key exists in an array. The syntax of this function is as follows:

<span><span><span class="hljs-keyword">bool</span></span><span> </span><span><span class="hljs-title function_ invoke__">key_exists</span></span><span> ( </span><span><span class="hljs-keyword">mixed</span></span><span> </span><span><span class="hljs-variable">$key</span></span><span> , </span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-variable">$array</span></span> )  
</span></span>
  • $key: The key to look for.

  • $array: The target array.

If the specified key exists in the array, key_exists() returns true, otherwise it returns false.

2. Basic Usage

<span><span><span class="hljs-variable">$array</span></span><span> = </span><span><span class="hljs-keyword">array</span></span><span>(</span><span><span class="hljs-string">"name"</span></span><span> =&gt; </span><span><span class="hljs-string">"John"</span></span><span>, </span><span><span class="hljs-string">"age"</span></span><span> =&gt; </span><span><span class="hljs-number">30</span></span><span>);  
<p></span>if (key_exists(</span>"name", </span>$array)) {<br>
</span>echo </span>"Key 'name' exists!";<br>
} </span>else {<br>
</span>echo </span>"Key 'name' does not exist!";<br>
}<br>
</span></span>

Output:

<span><span>Key </span><span><span class="hljs-string">'name'</span></span><span> exists!  
</span></span>

This example shows how to check if the key "name" exists in the array $array. If it does, the relevant message is displayed.

3. Distinguishing key_exists() from Other Methods

Although key_exists() is widely used, PHP offers several ways to check if a key exists, such as array_key_exists() and isset().

  • key_exists() vs array_key_exists()
    In fact, key_exists() and array_key_exists() are identical. key_exists() is simply an alias of array_key_exists(). They perform the same function, so you can use either based on personal preference or project guidelines.

  • isset() vs key_exists()
    isset() checks not only whether a key exists, but also whether its value is not null. If a key exists in the array but its value is null, isset() will return false. On the other hand, key_exists() only checks for the existence of the key itself, regardless of its value.

    <span><span><span class="hljs-variable">$array</span></span><span> = </span><span><span class="hljs-keyword">array</span></span>(</span><span><span class="hljs-string">"name"</span></span> =&gt; </span><span><span class="hljs-literal">null</span></span>);  
    
    </span><span><span class="hljs-keyword">if</span></span> (</span><span><span class="hljs-keyword">isset</span></span>(</span><span><span class="hljs-variable">$array</span></span>[</span><span><span class="hljs-string">"name"</span></span>])) {  
        </span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Key 'name' exists and is not null"</span></span>;  
    } </span><span><span class="hljs-keyword">else</span></span> {  
        </span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Key 'name' does not exist or is null"</span></span>;  
    }  
    </span></span>

    Output:

    <span><span>Key </span><span><span class="hljs-string">'name'</span></span><span> does not exist or is </span><span><span class="hljs-literal">null</span></span><span>  
    </span></span>

    If you only need to check whether the key exists regardless of whether its value is null, key_exists() is the better choice.

4. Usage with Multidimensional Arrays

When working with multidimensional arrays, key_exists() can also be used to check keys in nested arrays. You can chain multiple key_exists() calls to verify keys at deeper levels.

<span><span><span class="hljs-variable">$array</span></span> = </span><span><span class="hljs-keyword">array</span></span>(  
    </span><span><span class="hljs-string">"person"</span></span> =&gt; </span><span><span class="hljs-keyword">array</span></span>(</span><span><span class="hljs-string">"name"</span></span> =&gt; </span><span><span class="hljs-string">"Alice"</span></span>, </span><span><span class="hljs-string">"age"</span></span> =&gt; </span><span><span class="hljs-number">28</span></span>),  
    </span><span><span class="hljs-string">"job"</span></span> =&gt; </span><span><span class="hljs-keyword">array</span></span>(</span><span><span class="hljs-string">"title"</span></span> =&gt; </span><span><span class="hljs-string">"Engineer"</span></span>, </span><span><span class="hljs-string">"salary"</span></span> =&gt; </span><span><span class="hljs-number">5000</span></span>)  
);  

</span><span><span class="hljs-keyword">if</span></span> (</span><span><span class="hljs-title function_ invoke__">key_exists</span></span>(</span><span><span class="hljs-string">"name"</span></span>, </span><span><span class="hljs-variable">$array</span></span>[</span><span><span class="hljs-string">"person"</span></span>])) {  
    </span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Key 'name' exists in the person array!"</span></span>;  
} </span><span><span class="hljs-keyword">else</span></span> {  
    </span><span><span class="hljs-keyword">echo</span></span> </span><span><span class="hljs-string">"Key 'name' does not exist in the person array!"</span></span>;  
}  
</span></span>

Output:

<span><span>Key </span><span><span class="hljs-string">'name'</span></span><span> exists in the person array!  
</span></span>

This approach is straightforward and works well when checking keys in multidimensional arrays.

5. Performance Considerations

In PHP, the key_exists() function is typically very efficient. It operates directly on the internal structure of arrays, with a time complexity of O(1) (constant time). This makes it an excellent choice when you need to frequently check for the existence of keys.

However, keep in mind that in some scenarios, isset() may be slightly faster, especially when you want to verify both that a key exists and that its value is not null. Ultimately, your choice should depend on your specific needs.

6. Conclusion

key_exists() is a very practical PHP function for checking whether a key exists in an array. Whether you are working with one-dimensional or multidimensional arrays, it provides fast and accurate results. In most cases, key_exists() will serve your needs perfectly. Just remember its difference from isset() to avoid confusion. Choosing the right function will make your code cleaner and more efficient.

  • Related Tags:

    PDO