Arr Introduction

The Arr class offers static helpers for working with PHP arrays, including dot notation.

Syntax\Illuminate\Support\Arr::method($array, ...);

Illuminate\Support\Arr is a facade of static array helpers. Its superpower is dot notation: you can reach into nested arrays with a string like 'user.address.city'.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • A developer replaces verbose nested array_key_exists() and isset() chains with Arr::get() and dot notation to read deeply nested config values safely.
  • An import pipeline uses Arr::only() to whitelist exactly the fields it needs from an external API response, discarding undocumented or unexpected keys.
  • A configuration builder uses Arr::set() to programmatically write deeply nested keys into a settings array before persisting it to the database.

More examples

Import the Arr facade

Shows the Arr import and the most common method, Arr::get(), which reads a nested key via dot notation with an optional default.

Example · php
use Illuminate\Support\Arr;

$data = ['user' => ['name' => 'Alice', 'role' => 'admin']];

// Dot-notation access
$name = Arr::get($data, 'user.name');    // 'Alice'
$plan = Arr::get($data, 'user.plan', 'free'); // 'free' (default)

Add and modify nested keys safely

Arr::set() creates any missing intermediate keys automatically when writing a value at a dot-notation path.

Example · php
use Illuminate\Support\Arr;

$config = [];
Arr::set($config, 'database.host', 'localhost');
Arr::set($config, 'database.port', 3306);

// $config => ['database' => ['host' => 'localhost', 'port' => 3306]]

Check for key existence

Arr::has() checks for key presence via dot notation and can accept an array to verify that multiple keys all exist.

Example · php
use Illuminate\Support\Arr;

$payload = ['name' => 'Bob', 'email' => '[email protected]'];

Arr::has($payload, 'name');           // true
Arr::has($payload, 'phone');          // false
Arr::has($payload, ['name', 'email']); // true (all keys present)
Arr::exists($payload, 'email');       // true

Discussion

  • Be the first to comment on this lesson.