Helper Methods Flashcards
What does the Arr::accessible method do?
The Arr::accessible method determines if the given value is array accessible:
use Illuminate\Support\Arr; use Illuminate\Support\Collection; $isAccessible = Arr::accessible(['a' => 1, 'b' => 2]); // true $isAccessible = Arr::accessible(new Collection); // true $isAccessible = Arr::accessible('abc'); // false $isAccessible = Arr::accessible(new stdClass); // false
What does the Arr::add method do?
The Arr::add method adds a given key / value pair to an array if the given key doesn’t already exist in the array or is set to null:
use Illuminate\Support\Arr; $array = Arr::add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100] $array = Arr::add(['name' => 'Desk', 'price' => null], 'price', 100); // ['name' => 'Desk', 'price' => 100]
What does the Arr::collapse method do?
The Arr::collapse method collapses an array of arrays into a single array:
use Illuminate\Support\Arr; $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
What does the Arr::crossJoin method do?
The Arr::crossJoin method cross joins the given arrays, returning a Cartesian product with all possible permutations:
use Illuminate\Support\Arr; $matrix = Arr::crossJoin([1, 2], ['a', 'b']); /* [ [1, 'a'], [1, 'b'], [2, 'a'], [2, 'b'], ] */ $matrix = Arr::crossJoin([1, 2], ['a', 'b'], ['I', 'II']); /* [ [1, 'a', 'I'], [1, 'a', 'II'], [1, 'b', 'I'], [1, 'b', 'II'], [2, 'a', 'I'], [2, 'a', 'II'], [2, 'b', 'I'], [2, 'b', 'II'], ] */
What does the Arr::divide method do?
The Arr::divide method returns two arrays: one containing the keys and the other containing the values of the given array:
use Illuminate\Support\Arr; [$keys, $values] = Arr::divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk']
What does the Arr::dot method do?
The Arr::dot method flattens a multi-dimensional array into a single level array that uses “dot” notation to indicate depth:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $flattened = Arr::dot($array); // ['products.desk.price' => 100]
What does the Arr::except method do?
The Arr::except method removes the given key / value pairs from an array:
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100]; $filtered = Arr::except($array, ['price']); // ['name' => 'Desk']
What does the Arr::exists method do?
The Arr::exists method checks that the given key exists in the provided array:
use Illuminate\Support\Arr; $array = ['name' => 'John Doe', 'age' => 17]; $exists = Arr::exists($array, 'name'); // true $exists = Arr::exists($array, 'salary'); // false
What does the Arr::first method do?
The Arr::first method returns the first element of an array passing a given truth test:
use Illuminate\Support\Arr; $array = [100, 200, 300]; $first = Arr::first($array, function (int $value, int $key) { return $value >= 150; }); // 200
A default value may also be passed as the third parameter to the method. This value will be returned if no value passes the truth test:
use Illuminate\Support\Arr; $first = Arr::first($array, $callback, $default);
What does the Arr::flatten method do?
The Arr::flatten method flattens a multi-dimensional array into a single level array:
use Illuminate\Support\Arr; $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened = Arr::flatten($array); // ['Joe', 'PHP', 'Ruby']
What does the Arr::forget method do?
The Arr::forget method removes a given key / value pair from a deeply nested array using “dot” notation:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); // ['products' => []]
What does the Arr::get method do?
The Arr::get method retrieves a value from a deeply nested array using “dot” notation:
use Illuminate\Support\Arr; $array = ['products' => ['desk' => ['price' => 100]]]; $price = Arr::get($array, 'products.desk.price'); // 100
The Arr::get method also accepts a default value, which will be returned if the specified key is not present in the array:
use Illuminate\Support\Arr; $discount = Arr::get($array, 'products.desk.discount', 0); // 0
What does the Arr::has method do?
The Arr::has method checks whether a given item or items exists in an array using “dot” notation:
use Illuminate\Support\Arr; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = Arr::has($array, 'product.name'); // true $contains = Arr::has($array, ['product.price', 'product.discount']); // false
What does the Arr::hasAny method do?
The Arr::hasAny method checks whether any item in a given set exists in an array using “dot” notation:
use Illuminate\Support\Arr; $array = ['product' => ['name' => 'Desk', 'price' => 100]]; $contains = Arr::hasAny($array, 'product.name'); // true $contains = Arr::hasAny($array, ['product.name', 'product.discount']); // true $contains = Arr::hasAny($array, ['category', 'product.discount']); // false
What does the Arr::isAssoc method do?
The Arr::isAssoc method returns true if the given array is an associative array. An array is considered “associative” if it doesn’t have sequential numerical keys beginning with zero:
use Illuminate\Support\Arr; $isAssoc = Arr::isAssoc(['product' => ['name' => 'Desk', 'price' => 100]]); // true $isAssoc = Arr::isAssoc([1, 2, 3]); // false
What does the Arr::isList method do?
The Arr::isList method returns true if the given array’s keys are sequential integers beginning from zero:
use Illuminate\Support\Arr; $isList = Arr::isList(['foo', 'bar', 'baz']); // true $isList = Arr::isList(['product' => ['name' => 'Desk', 'price' => 100]]); // false
What does the Arr::join method do?
The Arr::join method joins array elements with a string. Using this method’s second argument, you may also specify the joining string for the final element of the array:
use Illuminate\Support\Arr; $array = ['Tailwind', 'Alpine', 'Laravel', 'Livewire']; $joined = Arr::join($array, ', '); // Tailwind, Alpine, Laravel, Livewire $joined = Arr::join($array, ', ', ' and '); // Tailwind, Alpine, Laravel and Livewire
What does the Arr::keyBy method do?
The Arr::keyBy method keys the array by the given key. If multiple items have the same key, only the last one will appear in the new array:
use Illuminate\Support\Arr; $array = [ ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ]; $keyed = Arr::keyBy($array, 'product_id'); /* [ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ] */
What does the Arr::last method do?
The Arr::last method returns the last element of an array passing a given truth test:
use Illuminate\Support\Arr; $array = [100, 200, 300, 110]; $last = Arr::last($array, function (int $value, int $key) { return $value >= 150; }); // 300
A default value may be passed as the third argument to the method. This value will be returned if no value passes the truth test:
use Illuminate\Support\Arr; $last = Arr::last($array, $callback, $default);
What does the Arr::map method do?
The Arr::map method iterates through the array and passes each value and key to the given callback. The array value is replaced by the value returned by the callback:
use Illuminate\Support\Arr; $array = ['first' => 'james', 'last' => 'kirk']; $mapped = Arr::map($array, function (string $value, string $key) { return ucfirst($value); }); // ['first' => 'James', 'last' => 'Kirk']
What does the Arr::mapSpread method do?
The Arr::mapSpread method iterates over the array, passing each nested item value into the given closure. The closure is free to modify the item and return it, thus forming a new array of modified items:
use Illuminate\Support\Arr; $array = [ [0, 1], [2, 3], [4, 5], [6, 7], [8, 9], ]; $mapped = Arr::mapSpread($array, function (int $even, int $odd) { return $even + $odd; }); /* [1, 5, 9, 13, 17] */
What does the Arr::mapWithKeys method do?
The Arr::mapWithKeys method iterates through the array and passes each value to the given callback. The callback should return an associative array containing a single key / value pair:
use Illuminate\Support\Arr; $array = [ [ 'name' => 'John', 'department' => 'Sales', 'email' => 'john@example.com', ], [ 'name' => 'Jane', 'department' => 'Marketing', 'email' => 'jane@example.com', ] ]; $mapped = Arr::mapWithKeys($array, function (array $item, int $key) { return [$item['email'] => $item['name']]; }); /* [ 'john@example.com' => 'John', 'jane@example.com' => 'Jane', ] */
What does the Arr::only method do?
The Arr::only method returns only the specified key / value pairs from the given array:
use Illuminate\Support\Arr; $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $slice = Arr::only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100]
What does the Arr::pluck method do?
The Arr::pluck method retrieves all of the values for a given key from an array:
use Illuminate\Support\Arr; $array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $names = Arr::pluck($array, 'developer.name'); // ['Taylor', 'Abigail']
You may also specify how you wish the resulting list to be keyed:
use Illuminate\Support\Arr; $names = Arr::pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']
- Name
- {{ fake()->name() }}
- {{ fake()->unique()->safeEmail() }}