PHP 8.3.4 Released!

array_key_exists

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

array_key_existsControlla se l'indice o la chiave specificato esiste nell'array

Descrizione

array_key_exists(string|int $key, array $array): bool

array_key_exists() restituisce true se la key data è impostata nell'array. key può essere qualsiasi valore possibile per un indice di array.

Elenco dei parametri

key

Il valore da verificare.

array

Un array con chiavi da controllare.

Valori restituiti

Restituisce true in caso di successo, false in caso di fallimento.

Nota:

array_key_exists() cercherà le chiavi solo nella prima dimensione. Le chiavi annidate negli array multidimensionali non verranno trovate.

Esempi

Example #1 Esempio di array_key_exists()

<?php
$search_array
= array('primo' => 1, 'secondo' => 4);
if (
array_key_exists('primo', $search_array)) {
echo
"L'elemento 'primo' è nell'array";
}
?>

Example #2 array_key_exists() vs isset()

isset() non restituisce true per le chiavi di array che corrispondono ad un valore null, mentre array_key_exists() lo fa.

<?php
$search_array
= array('primo' => null, 'secondo' => 4);

// restituisce false
isset($search_array['primo']);

// restituisce true
array_key_exists('primo', $search_array);
?>

Note

Nota:

Per motivi di retrocompatibilità, anche array_key_exists() restituirà true se key è una proprietà definita all'interno di un object dato come array. Questo comportamento è deprecato a partire da PHP 7.4.0 e rimosso a partire da PHP 8.0.0.

Per verificare se esiste una proprietà in un oggetto, dovrebbe essere utilizzata property_exists().

Vedere anche:

  • isset() - Verifica se una variabile è definita e non è null
  • array_keys() - Restituisce tutte le chiavi di un array
  • in_array() - Controlla se un valore è presente in un array
  • property_exists() - Checks if the object or class has a property

add a note

User Contributed Notes 3 notes

up
12
Julian
1 year ago
When you want to check multiple array keys:

<?php

$array
= [];
$array['a'] = '';
$array['b'] = '';
$array['c'] = '';
$array['d'] = '';
$array['e'] = '';

// all given keys a,b,c exists in the supplied array
var_dump(array_keys_exists(['a','b','c'], $array)); // bool(true)

function array_keys_exists(array $keys, array $array): bool
{
$diff = array_diff_key(array_flip($keys), $array);
return
count($diff) === 0;
}
up
4
Rumour
8 months ago
In PHP7+ to find if a value is set in a multidimensional array with a fixed number of dimensions, simply use the Null Coalescing Operator: ??

So for a three dimensional array where you are not sure about any of the keys actually existing

<?php

// instead of:
$exists = array_key_exists($key1, $arr) && array_key_exists($key2, $arr[$key1]) && array_key_exists($key3, $arr[$key1][$key2]) ;

// use:
$exists = array_key_exists($key3, $arr[$key1][$key2]??[]) ;

?>
up
-41
manhon824 at gmail dot com
12 years ago
I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.
To Top