AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP array_key_exists() Function



PHP Tutorial » PHP array key exists () Function

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

Syntax:

array_key_exists(key, array)

Parameters:

key -> Specifies the key
array -> Specifies an array

 

array_key_exists() - example

array_key_exists() will search for the keys in the first dimension only. in the next example check if the key "yellow" exists in an array:

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$a=array("red"=>"Ferrari","yellow"=>"Lamborghini", "blue"=>"Ford");
if (array_key_exists("yellow",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

</body>
</html>

Output:
Key exists!

 

PHP array key exists () More Examples

Check if the key "black" exists in an array:

Example:

<!DOCTYPE html>
<html>
<body>

<?php
$a=array("red"=>"Ferrari","yellow"=>"Lamborghini", "blue"=>"Ford");
if (array_key_exists("black",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>

</body>
</html>

Output:
Key does not exist!

 

What's the difference between isset() and array_key_exists() - example

array_key_exists will tell you if a key exists in an array return true or false, otherwise isset () will only return true if the key or variable exists in array and is not null.

Example:
$a = array('key1' => 'red', 'key2' => 'blue', 'key3' => null);

isset($a['key2']); // true
array_key_exists('key2', $a); // true

isset($a['key3']); // false
array_key_exists('key3', $a); // true

There is another important difference: isset doesn't complain when $a does not exist, while array_key_exists does.

 

PHP array key exists() Function, vs isset, multiple keys, and not empty, check, nested, in array, undefined index, case insensitive, PHP array_key_exists vs isset

PHP array key exists() Function, vs isset, multiple keys, and not empty, check, nested, in array, undefined index, case insensitive, PHP array_key_exists vs isset
PHP array_key_exists() Function - php tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...