This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
if you're using Composer to manage dependencies, you can include the following in your composer.json file:
"require": {
"mitsuru793/array-helper-function
}
Then, after running composer update or php composer.phar update, you can load the class using Composer's autoloading:
require 'vendor/autoload.php';
Finds whether a variable is a empty array.
return true when value is
[]
returns false when value is
- not array(ex:
0
,1
,true
,null
...) [1, 2]
[null]
[[], []]
is_empty_array([]) // true
Finds whether a variable is a full array, so it's not empty array.
return true when value is
[1, 2]
[null]
[[], []]
returns false when value is
[]
- not array(ex:
0
,1
,true
,null
...)
is_full_array([1]) // true
Finds whether all keys of variable are integer. This does not find deeply if element of $value
is array.
return true when value is
[]
[null]
[['k1' => 'v1'], 'v2]
returns false when value is
- not array(ex:
0
,1
,true
,null
...) ['k1' => 'v1', 'v2]
is_numeric_array([1]) // true
Finds whether all keys of variable are integer deeply. This is like is_numeric_array
return true when value is
[]
[null]
returns false when value is
- not array(ex:
0
,1
,true
,null
...) ['k1' => 'v1', 'v2]
[['k1' => 'v1'], 'v2]
is_numeric_array_recursive([1]) // true
Verify that all elements of a $array
pass a given truth $test
.
array_every([1, 2, 3], function ($value, $key) {
retrun $value > 2;
});
// false
If the $array
is empty, every will return true.
array_every([]) // true
If the $test
is null, verify all them are truly.
array_every([1, 2, 3]);
// true
Verify that a element of a $array
pass a given truth $test
.
array_every([1, 2, 3], function ($value, $key) {
retrun $value > 2;
});
// true
If the $array
is empty, every will return false.
array_every([]) // false
If the $test
is null, verify one is truly.
array_every([1, 2, 3]);
// true
This is like array_keys, but recursively. Return combinations of all keys.
$array = [
'Japan' => [
'Tokyo' => [
'user1' => 'Mike',
'user2' => 'Jane',
'Hiro',
'Hanako',
],
'Kyoto' => [],
],
'Take',
];
array_keys_recursive($array);
// [
// ['Japan', 'Tokyo', 'user1'],
// ['Japan', 'Tokyo', 'user2'],
// ['Japan', 'Tokyo', 0],
// ['Japan', 'Tokyo', 1],
// ['Japan', 'Kyoto'],
// 0,
// ]
This is like array_diff_key, but recursively.
$main = [
'user' => [
'name' => 'Mike',
'age' => 20,
],
];
$other = [
'id' => 2,
'user' => [
'name' => 'Jane',
'from' => 'America',
'sex' => 'woman',
],
];
array_diff_key_recursive($main, $other));
// [
// 'user' => [
// 'age' => 20
// ]
// ]
array_diff_key_recursive($other, $main));
// [
// 'id' => 2,
// 'user' => [
// 'from' => 'America',
// 'sex' => 'woman',
// ],
// ]
This is like array_filter, but recursively. The closure $test
is passed $key
by default.
$array = [
'Japan' => [
'Hiroki-Man',
'Kaede-Woman',
'Tokyo' => [
'Taro-Man',
'Hanako-Woman',
],
],
'America' => [
'Jane-Woman',
'Mike-Man',
],
];
array_filter_recursive($array, function ($v, $k) {
if (!is_string($v)) {
return true;
}
return preg_match('/Man/', $v);
});
// [
// 'Japan' => [
// 'Hiroki-Man',
// 'Tokyo' => [
// 'Taro-Man',
// ],
// ],
// 'America' => [
// 1 => 'Mike-Man',
// ],
// ]
Retrieves a value from a nested array. Notation of $path
is dot or array. If $path
is string, you can modify its separator from dot as 3rd argument.
$array = [
'user' => [
'name' => 'mike',
],
];
array_get($array, ['user', 'name']);
array_get($array, 'user.name');
array_get($array, 'user_name', '_');
// mike
array_get($array, 'invalid');
// null
Set a $value
within a nested array. Notation of $path
is dot or array. If $path
is string, you can modify its separator from dot as 3rd argument.
$array = [];
array_set($array, ['user', 'name'], 'Mike');
array_set($array, 'user.name', 'Mike');
array_set($array, 'user_name', 'Mike', '_');
$array;
// [
// 'user' => [
// 'name' => 'Mike'
// ]
// ]
Set a $value
within a nested array. Notation of $path
is dot or array. If $path
is string, you can modify its separator from dot as 3rd argument.
$array = [
'user' => [
'name' => 'Mike',
],
];
array_unset($array, ['user', 'name']);
array_unset($array, 'user.name');
array_unset($array, 'user_name', '_');
$array;
// [
// 'user' => []
// ]
Find whether $values
match elements of $array
strictly, and takes matched values from $array
. Has side effect and modify $array
.
$array = ['a', null, true, 'b'];
$picked;
// [0 => 'a', 2 => true]
$array;
// [1 => null, 3 => 'b']