📦 Arr Class
A utility class for working with arrays. Supports dot-notation access, structure checks, deep JSON decoding, key filtering, and key-based array merging.
🔑 get(array $array, string $key, $default = null)
Retrieves a value from an array using dot notation.
📌 Example:
$data = ['user' => ['name' => 'Alice']];
$name = Arr::get($data, 'user.name'); // 'Alice'📝 set(array &$array, string $key, $value): void
Sets a value in an array using dot notation.
📌 Example:
$data = [];
Arr::set($data, 'user.email', 'test@example.com');
// $data = ['user' => ['email' => 'test@example.com']]📋 isList(array $array): bool
Checks if the array is a list (sequential numeric keys starting from 0).
📌 Example:
Arr::isList(['a', 'b']); // true
Arr::isList(['a' => 1, 'b' => 2]); // false📁 isAssoc(array $array): bool
Checks if the array is associative (non-sequential or string keys).
📌 Example:
Arr::isAssoc(['a' => 1, 'b' => 2]); // true🌲 depth(array $array): int
Returns the maximum depth of nested arrays.
📌 Example:
Arr::depth([1, [2, [3]]]); // 3🧠 jsonDecode($data, $assoc = true, $depth = 512, $options = 0)
Recursively decodes JSON strings, even if they are nested in arrays.
📌 Example:
$json = '{"user":"{\"name\":\"Alice\"}"}';
$result = Arr::jsonDecode($json);
// ['user' => ['name' => 'Alice']]🚫 exceptKeys(array $array, array $keys): array
Removes the given keys from the array.
📌 Example:
$data = ['name' => 'Alice', 'email' => 'a@example.com'];
$result = Arr::exceptKeys($data, ['email']);
// ['name' => 'Alice']✅ onlyKeys(array $array, array $keys): array
Keeps only the specified keys in the array.
📌 Example:
$data = ['name' => 'Alice', 'email' => 'a@example.com'];
$result = Arr::onlyKeys($data, ['email']);
// ['email' => 'a@example.com']🔍 contains(array $array, string $fragment): bool
Whether any string value in the array contains the fragment. Non-string values are skipped, and nesting is not walked.
📌 Example:
Arr::contains(['one.css', 'two.js'], '.js'); // true🧹 clean(array $array): array
Drops entries whose value is empty — '', null, 0, false, []. A list is re-indexed afterwards, an associative array keeps its keys.
📌 Example:
Arr::clean(['a' => 1, 'b' => '', 'c' => []]); // ['a' => 1]
Arr::clean(['x', '', 'y']); // ['x', 'y']0 and false count as empty
It is empty(), so a legitimate zero disappears. Not for filtering numbers.
🥇 first(array $array, callable $callback = null, $default = null)
The first element, or the first one the callback accepts. Returns $default when nothing matches — including on an empty array, where reset() would hand back false.
📌 Example:
Arr::first([3, 8, 2]); // 3
Arr::first([3, 8, 2], fn($v) => $v > 5); // 8
Arr::first([], null, 'none'); // 'none'The callback receives the value and the key, in that order.
🔗 mergeByKeyMatch(array $base, array $extra, string $key): array
Merges two arrays by matching the value of a specific key.
📌 Example:
$base = [['id' => 1, 'name' => 'Alice'], ['id' => 2, 'name' => 'Bob']];
$extra = [['id' => 2, 'email' => 'bob@example.com']];
$result = Arr::mergeByKeyMatch($base, $extra, 'id');
/*
[
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com']
]
*/