Global array and value helpers of yf (functions/yf_array_funcs.php), each run here on the input of its code: the result
first, then the code that made it. Keys with dots (names.joe) reach into nested arrays. Signatures and docblocks of every
global helper: API reference / functions.
array_add
array_add($array, $key, $value): adds the key / value pair when the key is not in the array yet.
['foo' => 'bar', 'key' => 'value']array_add(['foo' => 'bar'], 'key', 'value')array_divide
array_divide($array): two arrays, the keys and the values ([$keys, $values] = array_divide($array)).
[['foo'], ['bar']]array_divide(['foo' => 'bar'])array_dot
array_dot($array, $prepend = ''): a nested array as one level with dotted keys.
['foo.bar' => 'baz']array_dot(['foo' => ['bar' => 'baz']])array_except
array_except($array, $keys): the array without the given keys.
['name' => 'Joe']array_except(['name' => 'Joe', 'age' => 27, 'votes' => 1], ['age', 'votes'])array_only
array_only($array, $keys): only the given keys.
['name' => 'Joe', 'votes' => 1]array_only(['name' => 'Joe', 'age' => 27, 'votes' => 1], ['name', 'votes'])array_fetch
array_fetch($array, $key): the nested value of a dotted key from each element, as a flat list.
['Taylor', 'Dayle']array_fetch([['developer' => ['name' => 'Taylor']], ['developer' => ['name' => 'Dayle']]], 'developer.name')array_pluck
array_pluck($array, $value, $key = null): one field of each element, keyed by another field when $key is given.
PHP's own array_column() does the same for arrays.
[7 => 'Taylor', 9 => 'Dayle']array_pluck([['id' => 7, 'name' => 'Taylor'], ['id' => 9, 'name' => 'Dayle']], 'name', 'id')array_flatten
array_flatten($array): all values of a nested array in one list.
['Joe', 'PHP', 'Ruby']array_flatten(['name' => 'Joe', 'languages' => ['PHP', 'Ruby']])array_get
array_get($array, $key, $default = null): a value by dotted key, or the default. object_get($object, $key, $default)
does the same for objects.
['programmer']array_get(['names' => ['joe' => ['programmer']]], 'names.joe')array_set
array_set(&$array, $key, $value): sets a value by dotted key, creating the levels on the way.
['names' => ['programmer' => 'Joe', 'editor' => 'Taylor']]$array = ['names' => ['programmer' => 'Joe']];
array_set($array, 'names.editor', 'Taylor');
return $array;array_forget
array_forget(&$array, $key): removes a value by dotted key.
['names' => ['jane' => ['designer']]]$array = ['names' => ['joe' => ['programmer'], 'jane' => ['designer']]];
array_forget($array, 'names.joe');
return $array;array_pull
array_pull(&$array, $key, $default = null): returns a value and removes it from the array.
['name' => 'Taylor', 'array' => ['age' => 27]]$array = ['name' => 'Taylor', 'age' => 27];
$name = array_pull($array, 'name');
return ['name' => $name, 'array' => $array];array_where
array_where($array, Closure $callback): the elements for which $callback($key, $value) is true, keys kept.
array_filter($array, fn ($value, $key) => ..., ARRAY_FILTER_USE_BOTH) is the plain PHP form (note the order of the
arguments).
[1 => '200', 3 => '400']array_where([100, '200', 300, '400', 500], fn ($key, $value) => is_string($value))array_first, array_last
array_first($array, $callback, $default = null) and array_last(...): the first / last value for which
$callback($key, $value) is true, else the default.
PHP 8.5 has its own array_first($array) and array_last($array) (the first / last
value, no callback). yf defines its helpers only where PHP has none, so on PHP 8.5 a call with a callback fails. Write code that runs
on every PHP yf supports (8.2 to 8.5) with array_filter(), or use array_find() from PHP 8.4 on:
['first' => 200, 'last' => 300]$prices = [100, 200, 300];
// every PHP 8.2+: the matches, then the first or last one or a default
$matches = array_values(array_filter($prices, fn ($price) => $price >= 150));
$first = $matches[0] ?? null;
$last = $matches ? end($matches) : null;
// PHP 8.4+ only: array_find($prices, fn ($price) => $price >= 150)
return ['first' => $first, 'last' => $last];head, last
head($array) / last($array): the first / last element without moving a variable's pointer (they take the
array by value, so they work on a function result).
foohead(['foo', 'bar', 'baz'])bazlast(['foo', 'bar', 'baz'])value
value($value): the result of a Closure, or the value itself; for options that may be either.
['bar', 'baz'][value(fn () => 'bar'), value('baz')]with
with($object): returns its argument. Older code used it to chain on new; since PHP 5.4
(new ArrayObject(...))->count() does the same.
3with(new ArrayObject(['a', 'b', 'c']))->count()