Blog de programación, errores, soluciones

Chose Language:
comments
Author: Admin/Publisher |finished | checked

PHP array_key_exists function

The PHP function array_key_exists checks whether the given value exists as a key in the provided array.

Description / Descripción
array_key_exists(string|int $key, array $array): bool

Parameters

$key – The value we will pass as a parameter to check if it exists as a key in the provided array.

$array – The array we provide for the check.

Returns

It will return true if there is a key with the value of $key, and false if there is not.

Example

We will continue with the car brands since it’s easy to use the same array:

$marcasDeAutos = array(
    "Toyota",
    "Ford",
    "Honda",
    "Chevrolet",
    "Volkswagen",
    "BMW",
    "Mercedes-Benz",
    "Audi",
    "Nissan",
    "Hyundai",
    "Kia",
    "Subaru",
    "Mazda",
    "Lexus",
    "Jeep",
);

if(array_key_exists("Ford",$marcasDeAutos)){
	echo "Ford es key del array";
}else{
	echo "Ford no es key del array";
}
Ford no es key del array

On the other hand, if we do something like this:

$auto = array(
    "marca" => "Toyota",
    "modelo" => "Corolla",
    "año" => 2023,
    "color" => "Plata",
    "precio" => 25000,
    "transmision" => "Automática",
    "motor" => "2.0L",
    "kilometraje" => 15000
);

if(array_key_exists("marca",$auto)){
	echo "marca es key del array";
}else{
	echo "marca no es key del array";
}
marca es key del array
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category

Comments