Oihana PHP

exists.php

Table of Contents

Functions

exists()  : bool
Checks whether a key exists in an array or an object implementing ArrayAccess.

Functions

exists()

Checks whether a key exists in an array or an object implementing ArrayAccess.

exists(array<string|int, mixed>|ArrayAccess $array, string|int|null $key[, string $separator = '.' ]) : bool
  • Returns false if the key is null or an empty string.
  • Supports both native arrays and ArrayAccess objects.
Parameters
$array : array<string|int, mixed>|ArrayAccess

The array or object to inspect.

$key : string|int|null

The key to check for existence.

$separator : string = '.'

The separator used in the key path. Default is '.'.

Tags
example
use oihana\core\arrays\exists;

// Flat array
$data = ['name' => 'Alice'];
var_dump(exists($data, 'name')); // true
var_dump(exists($data, 'age'));  // false

// Nested array
$data = ['user' => ['address' => ['country' => 'France']]];
var_dump(exists($data, 'user.address.country')); // true
var_dump(exists($data, 'user.address.city'));    // false

// Mixed with numeric keys
$data = ['items' => [0 => ['id' => 1], 1 => ['id' => 2]]];
var_dump(exists($data, 'items.1.id')); // true

// With null or empty key
var_dump(exists($data, null)); // false
var_dump(exists($data, ''));   // false
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
bool

True if the key exists, false otherwise.


        
On this page

Search results