• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php array_key_exists() Function – Check if Key Exists in Array

php array_key_exists() Function – Check if Key Exists in Array

April 7, 2022 Leave a Comment

The php array_key_exists() function checks an array to see if a given key exists in that array.

$array = array("black" => "bear", "grey" => "elephant", "yellow" => "lion");

var_dump(array_key_exists("black",$array));

// Output: 
bool(true)

When working with arrays in php, it can be valuable to be able to easily gather information of the properties of an array.

One such situation is if you want to know if a key exists in an array.

In php, we can use the array_key_exists() function to check an array for the existence of a specific key.

array_key_exists() takes two parameters. The first parameter is the value of the key you want to check, and the second parameter is the array to check.

If the key exists, array_key_exists() will return TRUE. If it doesn’t exist, array_key_exists() returns FALSE.

Below are some examples of how to use the php array_key_eixsts() function to find out if a key exists in an given array.

$array1 = array("black" => "bear", "grey" => "elephant", "yellow" => "lion");
$array2 = array(100,101,102);

var_dump(array_key_exists("black",$array1));
var_dump(array_key_exists(0,$array2));
var_dump(array_key_exists("pink",$array2));

// Output: 
bool(true)
bool(true)
bool(false)

Checking if a Key Exists in an Array with php isset() Function

You can also check if a key exists in an array with the php isset() function.

The isset() function in php takes one or more variables and checks to see if the variable is declared and is different than null.

isset() returns TRUE if the variable exists and FALSE if the variable hasn’t been declared or is null.

We can use isset() to check if a key in an array exists as shown in the following php code.

$array = array("black" => "bear", "grey" => "elephant", "yellow" => "lion");

var_dump(isset($array["black"]));

// Output: 
bool(true)

Other Articles You'll Also Like:

  • 1.  php array_slice() – Get Slice of Elements from Array
  • 2.  In PHP isset Method is Used For Checking if Variable is Defined
  • 3.  php atan2 – Find Arctangent of the Quotient of Two Numbers
  • 4.  php getimagesize – Get Height, Width and Image File Type
  • 5.  Check if String Starts with Given String in php with str_starts_with()
  • 6.  php Trig – Using Trigonometric Functions in php for Trigonometry
  • 7.  How to Get Current System IP Address in php
  • 8.  php pi – Get Value of pi Using php pi() Function
  • 9.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 10.  Get First Element of Array in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy