Jump to: navigation, search

Php array key exists

From w3cyberlearnings

Contents

PHP function array_key_exists

This function checks array key with a given array. If found, it will return true. If not found, it will return false.

Syntax array_key_exists

  • key: key to look for
  • array: array
array_key_exists(key,array);

Example 1


<?php

$student = array('Abbey' => 89, 'Abbie' => 88, 'Abbigail' => 83, 'Aiyana' => 93);

$key = 'Abbie';
if(array_key_exists($key, $student)) {
	echo "{$key} existed, and her score is {$student[$key]}<br/>";
}
?>

Output

Abbie existed, and her score is 88

Example 2


<?php

$fruits = array('orange', 'banana', 'apple');
$key = 0;
if (array_key_exists($key, $fruits)) {
	echo "{$fruits[$key]} is existed.<br/>";
} else {
	echo 'not existed.<br/>';
}
?>

Output

orange is existed.

Related Links


Navigation
Web
SQL
MISC
References