This function designed to get list of all values witch placed in $path
in $array
.
$data = [
'id' => 1,
'some_value' => 'qweqwe',
'nested_values' => [
[
'id' => 4,
'another_some_value' => 'value',
'next_level_of_nesting' => [
[
'id' => 6,
'value' => 'h'
],
[
'id' => 7,
'value' => 'e'
]
]
],
[
'id' => 5,
'another_some_value' => 'value',
'next_level_of_nesting' => [
[
'id' => 8,
'value' => 'l'
],
[
'id' => 9,
'value' => 'l'
]
]
],
[
'id' => 6,
'another_some_value' => 'value',
'next_level_of_nesting' => [
[
'id' => 10,
'value' => 'o'
]
]
]
]
];
$result = array_get_list($data, 'nested_values.*.next_level_of_nesting.*.value'); //['h', 'e', 'l', 'l', 'o'];
Verifies whether $array
is associative array or a list
$associative = [
'key' => 'value'
];
$list = ['some', 'values'];
is_associative($associative); //true
is_associative($list); //false
Return subtraction of $array2
from $array1
$array1 = [1, 2, 3];
$array2 = [1, 2];
$result = array_subtraction($array1, $array2); //[3]
Verifies whether two arrays are equals
$array1 = [1, 2, 3];
$array2 = [1, 2];
$array3 = [3, 2, 1];
array_equals($array1, $array3); //true
array_equals($array1, $array2); //false
Round all values in list of floats.
$array = [1.4, 2.9, 1.534];
array_round($array); //[1, 3, 2]
This feature is designed to get the first non-empty function result. It does not make sense in php7, but can be useful when developing applications on php5.
Example
$value = elseChain(
function() use ($request, $code) {
return empty($request) ? Response::$statusTexts[$code] : null;
},
function() use ($request, $code) {
return $this->annotationReader->getClassAnnotations($request)->get("_{$code}");
},
function() use ($code) {
return config("auto-doc.defaults.code-descriptions.{$code}");
},
function() use ($code) {
return Response::$statusTexts[$code];
}
);
Create directory recursively. The native mkdir() function recursively create directory incorrectly. Here is solution of this problem.
Remove directory recursively with all nested files and directories.
Generate GUID
Concat results of callback call. The first $array
argument should be an array of strings.
$array = ['some', 'random', 'values'];
$result = array_concat($array, function ($value, $key) {
return "{$key}. {$value}\n";
});
/**
0. some
1. random
2. values
*/
Remove all files and folders from $path
.
Builds an associative array by gotten keys and values.
$array = [
[
'id' => 1,
'value' => 'first'
],
[
'id' => 2,
'value' => 'second'
],
[
'id' => 3,
'value' => 'third'
]
];
$result = array_associate($array, function($value) {
return [
'key' => $value['id'],
'value' => $value['value']
];
});
//[1 => "first", 2 => "second", 3 => "third"]
Return duplicated values of input $array
.
$array = [1, 2, 2, 3];
array_duplicate($array);
//[2 => 2]
Return unique objects from array by field
>> $array = [
[
"id" => 1,
"value" => "first",
],
[
"id" => 2,
"value" => "second",
],
[
"id" => 2,
"value" => "second",
],
[
"id" => 3,
"value" => "third",
],
]
>>> $result = array_unique_object($array)
=> [
0 => [
"id" => 1,
"value" => "first",
],
1 => [
"id" => 2,
"value" => "second",
],
3 => [
"id" => 3,
"value" => "third",
],
]
inverse transformation from array_dot
>>> $array = [
'some.nested.value' => 1,
'some.array.0.value' => 2,
'some.array.1.value' => 3
];
>>> array_undot($array)
=> [
"some" => [
"nested" => [
"value" => 1,
],
"array" => [
[
"value" => 2,
],
[
"value" => 3,
],
],
],
]