Laravel Array Helper Functions: Usage and Examples
This article provides a comprehensive guide to Laravel's array helper functions, illustrating how to add, merge, retrieve, filter, and manipulate array data with practical PHP code examples for functions such as array_add, array_collapse, array_divide, array_dot, array_except, array_first, array_last, array_flatten, array_forget, array_get, array_has, array_only, array_pluck, array_prepend, array_pull, array_random, array_set, and array_sort.
This guide demonstrates the use of Laravel's array helper functions through clear PHP code snippets and explanations.
<?php $array = array_add([ 'name' => 'Desk' ], 'price', 100); // Result: ['name' => 'Desk', 'price' => 100]
The array_collapse function merges multiple arrays into a single flat array:
$array = array_collapse([[1,2,3],[4,5,6],[7,8,9]]); // Result: [1,2,3,4,5,6,7,8,9]
array_divide splits an associative array into two arrays, one containing keys and the other values:
list($keys, $values) = array_divide(['name' => 'Desk']); // $keys: ['name'], $values: ['Desk']
array_dot flattens a multi‑dimensional array using dot notation for nested keys:
$array = [ 'products' => [ 'desk' => [ 'price' => 100 ] ] ]; $flattened = array_dot($array); // Result: ['products.desk.price' => 100]
array_except removes specified key/value pairs from an array:
$array = ['name' => 'Desk', 'price' => 100]; $array = array_except($array, ['price']); // Result: ['name' => 'Desk']
array_first returns the first element that passes a given truth test, optionally providing a default value:
$array = [100,200,300]; $value = array_first($array, function($v){ return $v >= 150; }); // Result: 200
When no element satisfies the test, a default can be supplied:
$value = array_first($array, $callback, $default);
array_last works similarly but returns the last element that passes the test:
$value = array_last([100,200,300,110], function($v){ return $v >= 150; }); // Result: 300
array_flatten converts a multi‑dimensional array into a one‑dimensional array:
$array = ['name' => 'Joe', 'languages' => ['PHP','Ruby']]; $flat = array_flatten($array); // Result: ['Joe','PHP','Ruby']
array_forget removes a nested key using dot notation:
$array = ['products' => ['desk' => ['price' => 100]]]; array_forget($array, 'products.desk'); // Result: ['products' => []]
array_get retrieves a value from a nested array, with an optional default:
$value = array_get($array, 'products.desk.price'); // Result: 100 $value = array_get($array, 'products.desk.discount', 0); // Result: 0
array_has checks the existence of a key (or keys) in an array using dot notation:
$exists = array_has($array, 'product.name'); // true $existsAll = array_has($array, ['product.price','product.discount']); // false
array_only returns only the specified key/value pairs from an array:
$filtered = array_only($array, ['name','price']); // Result: ['name' => 'Desk', 'price' => 100]
array_pluck extracts a list of values for a given key from an array of arrays, optionally using another key as the result's keys:
$names = array_pluck($array, 'developer.name'); // Result: ['Taylor','Abigail'] $named = array_pluck($array, 'developer.name', 'developer.id'); // Result: [1 => 'Taylor', 2 => 'Abigail']
array_prepend adds an element to the beginning of an array, optionally specifying its key:
$array = ['one','two','three']; $array = array_prepend($array, 'zero'); // Result: ['zero','one','two','three'] $array = array_prepend(['price'=>100], 'Desk', 'name'); // Result: ['name'=>'Desk','price'=>100]
array_pull retrieves and removes a key/value pair from an array, with an optional default:
$name = array_pull($array, 'name'); // $name = 'Desk', $array now ['price'=>100] $value = array_pull($array, $key, $default);
array_random returns one or more random values from an array:
$random = array_random([1,2,3,4,5]); // e.g., 4 $items = array_random([1,2,3,4,5], 2); // e.g., [2,5]
array_set sets a value in a nested array using dot notation:
array_set($array, 'products.desk.price', 200); // Result: ['products'=>['desk'=>['price'=>200]]]
array_sort sorts an array by its values, optionally using a custom closure:
$sorted = array_sort(['Desk','Table','Chair']); // Result: ['Chair','Desk','Table'] $sorted = array_values(array_sort($array, function($v){ return $v['name']; }));
Each code block is presented within code tags to preserve formatting, and the surrounding paragraphs explain the purpose and output of the functions, providing a practical reference for developers working with Laravel's array utilities.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.