# Array Helper aka Arr
- Class with Namespace:
\FluentCrm\Framework\Support\Arr
- Method Types:
static
# 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:
use FluentCrm\Framework\Support\Arr;
$array = Arr::add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
2
3
4
5
# Arr::collapse()
The Arr::collapse
method collapses an array of arrays into a single array:
use FluentCrm\Framework\Support\Arr;
$array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
// [1, 2, 3, 4, 5, 6, 7, 8, 9]
2
3
4
5
# Arr::divide()
The Arr::divide
method returns two arrays, one containing the keys, and the other containing the values of the given array:
use FluentCrm\Framework\Support\Arr;
[$keys, $values] = Arr::divide(['name' => 'Desk']);
// $keys: ['name']
// $values: ['Desk']
2
3
4
5
6
7
# Arr::dot()
The Arr::dot
method flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth:
use FluentCrm\Framework\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$flattened = Arr::dot($array);
// ['products.desk.price' => 100]
2
3
4
5
6
7
# Arr::except()
The Arr::except
method removes the given key / value pairs from an array:
use FluentCrm\Framework\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$filtered = Arr::except($array, ['price']);
// ['name' => 'Desk']
2
3
4
5
6
7
# Arr::first()
The Arr::first
method returns the first element of an array passing a given truth test:
use FluentCrm\Framework\Support\Arr;
$array = [100, 200, 300];
$first = Arr::first($array, function ($value, $key) {
return $value >= 150;
});
// 200
2
3
4
5
6
7
8
9
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 FluentCrm\Framework\Support\Arr;
$first = Arr::first($array, $callback, $default);
2
3
# Arr::flatten()
The Arr::flatten
method flattens a multi-dimensional array into a single level array:
use FluentCrm\Framework\Support\Arr;
$array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
$flattened = Arr::flatten($array);
// ['Joe', 'PHP', 'Ruby']
2
3
4
5
6
7
# Arr::forget()
The Arr::forget
method removes a given key / value pair from a deeply nested array using "dot" notation:
use FluentCrm\Framework\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::forget($array, 'products.desk');
// ['products' => []]
2
3
4
5
6
7
# Arr::get()
The Arr::get
method retrieves a value from a deeply nested array using "dot" notation:
use FluentCrm\Framework\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
$price = Arr::get($array, 'products.desk.price');
// 100
2
3
4
5
6
7
The Arr::get
method also accepts a default value, which will be returned if the specific key is not found:
use FluentCrm\Framework\Support\Arr;
$discount = Arr::get($array, 'products.desk.discount', 0);
// 0
2
3
4
5
# Arr::has()
The Arr::has
method checks whether a given item or items exists in an array using "dot" notation:
use FluentCrm\Framework\Support\Arr;
$array = ['product' => ['name' => 'Desk', 'price' => 100]];
$contains = Arr::has($array, 'product.name');
// true
$contains = Arr::has($array, ['product.price', 'product.discount']);
// false
2
3
4
5
6
7
8
9
10
11
# Arr::last()
The Arr::last
method returns the last element of an array passing a given truth test:
use FluentCrm\Framework\Support\Arr;
$array = [100, 200, 300, 110];
$last = Arr::last($array, function ($value, $key) {
return $value >= 150;
});
// 300
2
3
4
5
6
7
8
9
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 FluentCrm\Framework\Support\Arr;
$last = Arr::last($array, $callback, $default);
2
3
# Arr::only()
The Arr::only
method returns only the specified key / value pairs from the given array:
use FluentCrm\Framework\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
$slice = Arr::only($array, ['name', 'price']);
// ['name' => 'Desk', 'price' => 100]
2
3
4
5
6
7
# Arr::pluck()
The Arr::pluck
method retrieves all of the values for a given key from an array:
use FluentCrm\Framework\Support\Arr;
$array = [
['developer' => ['id' => 1, 'name' => 'Jewel']],
['developer' => ['id' => 2, 'name' => 'Adre']],
];
$names = Arr::pluck($array, 'developer.name');
// ['Jewel', 'Adre']
2
3
4
5
6
7
8
9
10
You may also specify how you wish the resulting list to be keyed:
use FluentCrm\Framework\Support\Arr;
$names = Arr::pluck($array, 'developer.name', 'developer.id');
// [1 => 'Jewel', 2 => 'Adre']
2
3
4
5
# Arr::prepend()
The Arr::prepend
method will push an item onto the beginning of an array:
use FluentCrm\Framework\Support\Arr;
$array = ['one', 'two', 'three', 'four'];
$array = Arr::prepend($array, 'zero');
// ['zero', 'one', 'two', 'three', 'four']
2
3
4
5
6
7
If needed, you may specify the key that should be used for the value:
use FluentCrm\Framework\Support\Arr;
$array = ['price' => 100];
$array = Arr::prepend($array, 'Desk', 'name');
// ['name' => 'Desk', 'price' => 100]
2
3
4
5
6
7
# Arr::pull()
The Arr::pull
method returns and removes a key / value pair from an array:
use FluentCrm\Framework\Support\Arr;
$array = ['name' => 'Desk', 'price' => 100];
$name = Arr::pull($array, 'name');
// $name: Desk
// $array: ['price' => 100]
2
3
4
5
6
7
8
9
A default value may be passed as the third argument to the method. This value will be returned if the key doesn't exist:
use FluentCrm\Framework\Support\Arr;
$value = Arr::pull($array, $key, $default);
2
3
# Arr::random()
The Arr::random
method returns a random value from an array:
use FluentCrm\Framework\Support\Arr;
$array = [1, 2, 3, 4, 5];
$random = Arr::random($array);
// 4 - (retrieved randomly)
2
3
4
5
6
7
You may also specify the number of items to return as an optional second argument. Note that providing this argument will return an array, even if only one item is desired:
use FluentCrm\Framework\Support\Arr;
$items = Arr::random($array, 2);
// [2, 5] - (retrieved randomly)
2
3
4
5
# Arr::set()
The Arr::set
method sets a value within a deeply nested array using "dot" notation:
use FluentCrm\Framework\Support\Arr;
$array = ['products' => ['desk' => ['price' => 100]]];
Arr::set($array, 'products.desk.price', 200);
// ['products' => ['desk' => ['price' => 200]]]
2
3
4
5
6
7
# Arr::sort()
The Arr::sort
method sorts an array by its values:
use FluentCrm\Framework\Support\Arr;
$array = ['Desk', 'Table', 'Chair'];
$sorted = Arr::sort($array);
// ['Chair', 'Desk', 'Table']
2
3
4
5
6
7
You may also sort the array by the results of the given Closure:
use FluentCrm\Framework\Support\Arr;
$array = [
['name' => 'Desk'],
['name' => 'Table'],
['name' => 'Chair'],
];
$sorted = array_values(Arr::sort($array, function ($value) {
return $value['name'];
}));
/*
[
['name' => 'Chair'],
['name' => 'Desk'],
['name' => 'Table'],
]
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Arr::sortRecursive()
The Arr::sortRecursive
method recursively sorts an array using the sort
function for numeric sub=arrays and ksort
for associative sub-arrays:
use FluentCrm\Framework\Support\Arr;
$array = [
['Roman', 'Taylor', 'Li'],
['PHP', 'Ruby', 'JavaScript'],
['one' => 1, 'two' => 2, 'three' => 3],
];
$sorted = Arr::sortRecursive($array);
/*
[
['JavaScript', 'PHP', 'Ruby'],
['one' => 1, 'three' => 3, 'two' => 2],
['Li', 'Roman', 'Taylor'],
]
*/
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Arr::where()
The Arr::where
method filters an array using the given Closure:
use FluentCrm\Framework\Support\Arr;
$array = [100, '200', 300, '400', 500];
$filtered = Arr::where($array, function ($value, $key) {
return is_string($value);
});
// [1 => '200', 3 => '400']
2
3
4
5
6
7
8
9