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

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.


Send forgot password by mail or message ...
PHP MySQL simple login system...
Check for duplicate username live or ema...
How to create simple search engine using...
Diseño de sitio web CSS Layout...
Diseño de caja flexible CSS3...
Botones en CSS...
ID y Clase, Diferencias entre Class e Id...
Esquina redondeada CSS...
Tamaño de caja css box-sizing: border-b...
Alineación CSS...
Altura y Anchura CSS...
Bordes CSS...
Margen CSS...
Relleno CSS...
Modelo de caja CSS...
Tablas CSS...
Listas CSS...
Enlaces CSS...
Texto CSS...