Menu

Laravel Array Helpers

Hello buddies,
Today we will learn about some of the laravel helpers. But our main topic is Laravel array helpers.  These helpers are helpful for modifying arrays and strings. I will show some examples from laravel's official website.

Let's Start.

Arr::accessible()

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

 

Arr::add()

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]

 

Arr::collapse()

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]

 

Arr::except()

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']

 

Arr::exists()

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

 

Arr::flatten()

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']

 

Arr::get()

The Arr::get the 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

 

Arr::has()

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

 

Arr::hasAny()

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

 

Arr::last()

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 ($value, $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);

 

Arr::pluck()

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']

These are a few examples of the laravel array helper.
Source: Laravel

Thanks

794
Search

Ads