Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Useful Laravel array utility functions

Friday, August 14th, 2015 by Servage

laravelLaravel includes many useful functions. This article contains a list of great utilities for handling arrays.

The function “array_add” inserts a key with a corresponding value to a given array if the key is not already present in the array.

$values = ['name' => 'John'];
$array = array_add( $values, 'age', 35 );

// Value of $array:
// ['name' => 'John', 'age' => 35]

The function “array_divide” returns two arrays with the keys and values of the original array respectively.

list($keys, $values) = array_divide(['name' => 'John']);

// Output values:
// $keys: ['name']
// $values: ['John']

The function “array_dot” turns a multi-dimensional array into a flattened one-dimensional array using dot-notation to indicate the old depth:

$array = array_dot(['foo' => ['bar' => 'baz']]);

// Value of array:
// ['foo.bar' => 'baz'];

The function “array_except” deletes a key plus the corresponding value from a given array:

$array = ['name' => 'John', 'age' => 35];
$array = array_except($array, ['price']);

// New value of $array:
// ['name' => 'Desk']

The function “array_first” returns the first element which passed the given callback test (a default value can be added as a third argument after the callback, to be used in case no array value matches the test):

$array = [100, 200, 300];

$value = array_first($array, function ($key, $value)
{
	return $value >= 150;
});

// Returns:
// 200

The function “array_flatten” will turn a multi-dimensional array into a one-dimensional array consisting of all the values from the different dimensions:

$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$array = array_flatten($array);

// New value of $array:
// ['Joe', 'PHP', 'Ruby'];

The function “array_forget” deletes the value for a specific key in a multi-dimensional array using dot-notation for the key.

$array = ['products' => ['desk' => ['price' => 100]]];
array_forget($array, 'products.desk');

// New value of $array:
// ['products' => []]

The function “array_get” selects a value from a multi-dimensional array using dot-notation (a last argument can be added as default value, in case nothing is found with the dotted key):

$array = ['products' => ['desk' => ['price' => 100]]];
$array = array_get($array, 'products.desk');

// New value of $array:
// ['price' => 100]

The function “array_only” will return the keys with values for a given set of keys:

$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$array = array_only($array, ['name', 'price']);

// Value of $array:
// ['name' => 'Desk', 'price' => 100]

The function “array_pluck” will select a list of the given values based on dot-notation:

$array = [
	['developer' => ['name' => 'Taylor']],
	['developer' => ['name' => 'Abigail']]
];

$array = array_pluck($array, 'developer.name');

// Value of $array:
// ['Taylor', 'Abigail'];

The function “array_pull” returns the value, and deletes the key plus value from the original array:

$array = ['name' => 'Desk', 'price' => 100];
$name = array_pull($array, 'name');

// Value of $name:
// $name: Desk

// Value of $array after pull:
// $array: ['price' => 100]

The function “array_set” will override the value for a key with dot-notation in a multi-dimensional array:

$array = ['products' => ['desk' => ['price' => 100]]];
array_set($array, 'products.desk.price', 200);

// New value of $array:
// ['products' => ['desk' => ['price' => 200]]]

The function “array_sort” will sort the array based on the provided callback method:

$array = [
	['name' => 'Desk'],
	['name' => 'Chair'],
];

$array = array_values(array_sort($array, function ($value)
{
	return $value['name'];
}));

// Value of $array
/*
[
	['name' => 'Chair'],
	['name' => 'Desk'],
]
*/

The function “array_sort_recursive” will sort the array based on the default PHP sort function:

$array = [
	[
		'Roman',
		'Taylor',
		'Li',
	],
	[
		'PHP',
		'Ruby',
		'JavaScript',
	],
];

$array = array_sort_recursive($array);

// New value of $array:
/*
[
	[
		'Li',
		'Roman',
		'Taylor',
	],
	[
		'JavaScript',
		'PHP',
		'Ruby',
	]
];
*/

The function “array_where” filters the array with a callback function:

$array = [100, '200', 300, '400', 500];

$array = array_where($array, function ($key, $value)
{
	return is_string($value);
});

// New value of $array:
// [1 => 200, 3 => 400]

The function “head” returns the first element of an array:

$array = [100, 200, 300];
$first = head($array);

// Value of $first
// 100

The function “last” returns the last element of an array:

$array = [100, 200, 300];
$last = last($array);

// Value of $last
// 300

All these array functions are very useful and provide some functionality beyond the default PHP array implementation. I suggest you study them briefly to become familiar, and then you can always learn more in depth for the ones you find a use case for in your code. Using these utility functions can reduce your programming time.

Useful Laravel array utility functions, 4.3 out of 5 based on 6 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.