Arr クラス
Arr クラスは、配列を扱うヘルパー関数を集めたものです。
is_multi($arr, $all_keys = false)
is_multi メソッドは、与えられた配列が多次元配列であるかチェックします。
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$arr |
必須 |
チェックする配列。 |
$all_keys |
false
|
trueの場合は全ての要素が多次元配列かをチェックします。 |
|
返り値 |
bool |
例 |
// Single array
$arr = array('one' => 1, 'two' => 2);
echo Arr::is_multi($arr);
// 実行結果: false
// 多次元配列
$arr = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => array('test' => 3));
echo Arr::is_multi($arr);
// 実行結果: true
// 多次元配列で全ての要素をチェック
$arr = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => 3);
echo Arr::is_multi($arr, false); // 実行結果: true
echo Arr::is_multi($arr, true); // 実行結果: false
|
is_assoc($arr)
is_assoc メソッドは、与えられた配列が連想配列であるかチェックします。
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$arr |
必須 |
チェックする配列。 |
|
返り値 |
bool |
例 |
$arr = array('foo', 'bar', 'baz', 'yay');
echo Arr::is_assoc($arr);
// 実行結果: false
$arr = array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz', 'yay' => 'yay');
echo Arr::is_assoc($arr);
// 実行結果: true
/*
* たとえもし '2' が string で定義されていたとしても、PHPは内部的に integer として保管するため、
* 連想配列とは認識されません!
*/
$arr = array(0 => 'foo', 1 => 'bar', '2' => 'baz', 3 => 'yay');
echo Arr::is_assoc($arr);
// 実行結果: false!
|
to_assoc($arr)
to_assoc メソッドは連想配列でない配列を連想配列に変換します。もし元の配列に数値が入っていたとしても変換します。
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$arr |
必須 |
変換する配列。 |
|
返り値 |
array|null |
例外 |
BadMethodCallException 引数の配列の要素数が
奇数のとき |
例 |
$arr = array('foo', 'bar', 'baz', 'yay');
print_r(Arr::to_assoc($arr));
// 実行結果:
Array
(
["foo"] => 'bar'
["baz"] => 'yay'
)
$arr = array('foo', 'bar', 'baz');
echo Arr::to_assoc($arr);
// 実行結果: null
|
assoc_to_keyval($assoc = null, $key_field = null, $val_field = null)
assoc_to_keyval メソッドは多次元配列を key=>val の配列に変換します。
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$assoc |
必須 |
変換する配列。 |
$key_field |
必須 |
key となる連想配列の添字。 |
$val_field |
必須 |
value となる連想配列の添字。 |
|
返り値 |
array |
例外 |
InvalidArgumentException 1番目の引数が配列でないか、
Iterator インターフェイスを実装していないとき。 |
例 |
$people = array(
array(
"name" => "Jack",
"age" => 21
),
array(
"name" => "Jill",
"age" => 23
)
);
print_r(Arr::assoc_to_keyval($people, 'name', 'age'));
// 実行結果:
Array
(
["Jack"] => 21
["Jill"] => 23
)
|
average($array = null)
average メソッドは全ての配列の値から平均値を求めて返します。
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
平均値を求める配列。 |
|
返り値 |
array |
例 |
echo Arr::average(array('1', 2, 4, '8'));
// 実行結果: 3.75
|
flatten($array, $glue = ':', $reset = true)
The flatten method flattens a multi-dimensional array (both associative and indexed) down into a 1 dimensional array.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to flatten. |
$glue |
:
|
The string used to glue the keys together with |
$reset |
true
|
Should we create a new array of values instead of merging them into the last array created by flatten? |
|
返り値 |
array |
例 |
$indexed = array(
array("a"),
array("b"),
array("c"),
);
print_r(Arr::flatten($indexed, '_'));
// Result:
Array
(
[0_0] => a
[0_1] => b
[0_2] => c
)
|
flatten_assoc($array, $glue = ':', $reset = true)
The flatten_assoc method flattens a multi-dimensional associative array down into a 1 dimensional associative array.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to flatten. |
$glue |
:
|
The string used to glue the keys together with |
$reset |
true
|
Should we create a new array of values instead of merging them into the last array created by flatten_assoc? |
|
返り値 |
array |
例 |
$people = array(
array(
"name" => "Jack",
"age" => 21
),
array(
"name" => "Jill",
"age" => 23
)
);
print_r(Arr::flatten_assoc($people));
// Result:
Array
(
[0:name] => Jack
[0:age] => 21
[1:name] => Jill
[1:age] => 23
)
// Let's flatten another array on top
print_r(Arr::flatten_assoc(array(array("name" => "Humpty", "age" => 11)), ":", false));
// Result:
Array
(
[0:name] => Humpty
[0:age] => 11
[1:name] => Jill
[1:age] => 23
)
|
reverse_flatten($array, $glue = ':')
The reverse_flatten method unflattens a flattened multi-dimensional array (both associative and indexed) into its original form.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to unflatten. |
$glue |
:
|
The string used to glue the keys together with |
|
返り値 |
array |
例 |
$flattened = array(
'0_name' => 'James',
'0_age' => 24,
'1_name' => 'John',
'1_age' => 34,
);
print_r(Arr::reverse_flatten($flattened, '_'));
// Result:
Array
(
[0] => Array
(
[name] => James
[age] => 24
)
[1] => Array
(
[name] => John
[age] => 34
)
)
|
filter_prefixed($array, $prefix, $remove_prefix = true)
The filter_prefixed method filters the array on a prefix. It returns an array where the key starts with the specified prefix.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to filter. |
$prefix |
required |
The string used to filter on |
$remove_prefix |
true
|
Remove or keep the prefix in the array key? |
|
返り値 |
array |
例 |
$arr = array(
"user_name" => "John",
"user_surname" => "Lastname",
"project_name" => "Fuel",
"project_type" => "Framework",
);
print_r(Arr::filter_prefixed($arr, "user_"));
// Result:
Array
(
[name] => John
[surname] => Lastname
)
// Let's keep the prefix
print_r(Arr::filter_prefixed($arr, "project_", false);
// Result:
Array
(
[project_name] => Fuel
[project_type] => Framework
)
|
remove_prefixed($array, $prefix)
The remove_prefixed method removes values from an array if they match a given prefix.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to remove from. |
$prefix |
required |
The string used to filter on |
|
返り値 |
array |
例 |
$arr = array(
"user_name" => "John",
"user_surname" => "Lastname",
"project_name" => "Fuel",
"project_type" => "Framework",
);
print_r(Arr::remove_prefixed($arr, "project"));
// Result:
Array
(
[user_name] => John
[user_surname] => Lastname
)
|
filter_suffixed($array, $suffix, $remove_suffix = true)
The filter_suffixed method filters the array on a suffix. It returns an array where the key ends with the specified suffix.
静的 |
はい |
Parameters |
Param |
Default |
Description |
$array |
required |
The array to filter. |
$suffix |
required |
The string used to filter on |
$remove_suffix |
true
|
Remove or keep the suffix in the array key? |
|
Returns |
array |
Example |
$arr = array(
"name_1" => "John",
"surname_1" => "Lastname",
"name_2" => "Ted",
"surname_2" => "Surname",
);
print_r(Arr::filter_suffixed($arr, "_1"));
// Result:
Array
(
[name] => John
[surname] => Lastname
)
// Let's keep the suffix
print_r(Arr::filter_suffixed($arr, "_1", false);
// Result:
Array
(
[name_1] => John
[surname_1] => Lastname
)
|
remove_suffixed($array, $suffix)
The remove_suffixed method removes values from an array if they match a given suffix.
静的 |
はい |
Parameters |
Param |
Default |
Description |
$array |
required |
The array to remove from. |
$suffix |
required |
The string used to filter on |
|
Returns |
array |
Example |
$arr = array(
"name_1" => "John",
"surname_1" => "Lastname",
"name_2" => "Ted",
"surname_2" => "Bear",
);
print_r(Arr::remove_suffixed($arr, "_1"));
// Result:
Array
(
[name_2] => Ted
[surname_2] => Surname
)
|
filter_keys($array, $keys, $remove = false)
The filter_keys method filters a given array to a set of keys. It returns an array that contains only the items whose keys are in the $keys array. Can also remove the specified $keys from an array.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to filter. |
$keys |
必須 |
Array of keys to filter the above array with. |
$remove |
false
|
If true, removes the $keys from the $array instead of fetching them. |
|
返り値 |
array |
例 |
$arr = array(
"user_name" => "John",
"user_surname" => "Lastname",
"project_name" => "Fuel",
"project_type" => "Framework",
);
print_r(Arr::filter_keys($arr, array('project_name', 'user_name')));
// Result:
Array
(
[project_name] => Fuel
[user_name] => John
)
// Let's remove some keys
print_r(Arr::filter_keys($arr, array('user_name', 'user_surname'), true));
// Result:
Array
(
[project_name] => Fuel
[project_type] => Framework
)
|
filter_recursive($array, $callback = null)
The filter_recursive method provides a recursive version of PHP's array_filter() function. Like it's counterpart, you can optionally pass a callback function to determine what should be filtered.
静的 |
はい |
Parameters |
Param |
Default |
Description |
$array |
required |
The array to filter. |
$callback |
null
|
A valid callback that returns a boolean. |
|
Returns |
array |
Example |
$arr = array(
"user_name" => "John",
"user_surname" => "Lastname",
"info" => array(
0 => array(
"data" = "a value",
),
1 => array(
"data" = "",
),
2 => array(
"data" = 0,
),
),
);
print_r(Arr::filter_recursive($arr));
// Result:
Array
(
[project_name] => Fuel
[user_name] => John
[info] => Array
(
[0] => Array
(
[data] => a value
)
)
)
// Let's use a callback
print_r(Arr::filter_recursive($arr, function($item){ return $item !== ""; });
// Result:
Array
(
[project_name] => Fuel
[user_name] => John
[info] => Array
(
[0] => Array
(
[data] => a value
)
[2] => Array
(
[data] => 0
)
)
)
|
get($array, $key, $default = false)
The get method returns the element of the given array using dot-notation, or a default if it is not set.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to access |
$key |
必須 |
The key requested in $array. If null is passed as key, the entire array is returned. |
$default |
false
|
The value to be returned if the requested key does not exist |
|
返り値 |
mixed. If you pass an array of keys, the return value will be an array with the result of all requested keys. |
例 |
$person = array(
"name" => "Jack",
"age" => "21",
"location" => array(
"city" => "Pittsburgh",
"state" => "PA",
"country" => "US"
)
);
echo Arr::get($person, "name", "Unknown Name");
// Result: "Jack"
echo Arr::get($person, "job", "Unknown job");
// Result: "Unknown job"
// This method can also dive into arrays by using a dot to separate between keys
echo Arr::get($person, "location.city", "Unknown City");
// Result: "Pittsburgh"
|
set(&$array, $key, $value = null)
The set method sets the element of the given array using dot-notation.
WARNING: The original array is edited by reference
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to access |
$key |
必須 |
The key requested in $array. If no key is passed, or null is passed as key value, the array is unset. If you pass an array of key -> value pairs, all the values in the array will be set. |
$value |
null
|
The value to be set. Ignored if $key is an array. |
|
返り値 |
void |
例 |
$person = array(
"name" => "Jack",
"age" => "21",
"location" => array(
"city" => "Pittsburgh",
"state" => "PA",
"country" => "US"
)
);
Arr::set($person, "name", "John");
// $person['name'] is set to "John"
// This method can also dive into arrays by using a dot to separate between keys
Arr::set($person, "location.city", "Philadelphia");
// $person['location']['city'] is set to "Philadelphia"
// or set multiple values in one go
Arr::set($person, array("name" => "John", "location.city" => "Philadelphia"));
|
pluck($array, $key, $index = null)
The pluck method plucks values from a collection of arrays or objects.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to pluck from |
$key |
必須 |
The key to pluck from the arrays. |
$index |
null
|
Optional return array index. |
|
返り値 |
void |
例 |
$collection = array(
array(
'id' => 2,
'name' => 'Bill',
'surname' => 'Cosby',
),
array(
'id' => 5,
'name' => 'Chris',
'surname' => 'Rock',
),
array(
'id' => 7,
'name' => 'Bert',
'surname' => 'Visser',
),
);
// Get an array of id's
$ids = \Arr::pluck($collection, 'id');
// array(2, 5, 7);
// Get an array of names with the id as the index
$names = \Arr::pluck($collection, 'name', 'id');
// array(2 => 'Bill', 5 => 'Chris', 7 => 'Bert');
|
delete(&$array, $key)
The delete method the element of the given array using dot-notation.
WARNING: The original array is edited by reference, only boolean success is returned
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to access |
$key |
必須 |
The key requested in $array. If no key is passed, or null is passed as key value, the array is unset. |
|
返り値 |
mixed, true if the key was deleted, false if the key didn't exist. If you pass an array of keys, the return value will be an array with the result of all requested deletes. |
例 |
$person = array(
"name" => "Jack",
"age" => "21",
"location" => array(
"city" => "Pittsburgh",
"state" => "PA",
"country" => "US"
)
);
$result = Arr::delete($person, "name");
// deleted $person['name'] from the array, returns true
// This method can also dive into arrays by using a dot to separate between keys
$result = Arr::delete($person, "location.city");
// deleted $person['location']['city'] from the array, returns true
// or delete multiple values in one go
$result = Arr::delete($person, array("name", "location.doesnotexist"));
// deleted $person['name'], returns array('name' => true, 'location.doesnotexist' => false)
|
insert(Array &$original, $value, $pos)
The insert method is mainly an array_splice alias with added error checking
WARNING: The original array is edited by reference, only boolean success is returned
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$original |
必須 |
The array to use |
$value |
必須 |
The value(s) to insert |
$pos |
必須 |
The numeric position at which to insert, negative to count from the end backwards |
|
返り値 |
boolean |
例 |
$people = array("Jack", "Jill");
// Add one value
Arr::insert($people, "Humpty", 0);
print_r($people);
// Result:
Array
(
[0] => Humpty
[1] => Jack
[2] => Jill
)
// Add multiple values
Arr::insert($people, array("Hansel", "Gretel"), 1);
print_r($people);
// Result:
Array
(
[0] => Humpty
[1] => Hansel
[2] => Gretel
[3] => Jack
[4] => Jill
)
// Add an array
Arr::insert($people, array( array("name" => "Wolf", "teeth" => "sharp")), 0);
print_r($people);
// Result:
Array
(
[0] => Array
(
[name] => Wolf
[teeth] => sharp
)
[1] => Humpty
[2] => Hansel
[3] => Gretel
[4] => Jack
[5] => Jill
)
|
insert_before_key(Array &$original, $value, $key)
The insert_before_key method adds an element to an array before the key specified
WARNING: The original array is edited by reference, only boolean success is returned
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$original |
必須 |
The array to use |
$value |
必須 |
The value(s) to insert |
$key |
必須 |
The key before which to insert |
|
返り値 |
boolean |
例 |
$people = array("Jack", "Jill");
Arr::insert_before_key($people, "Humpty", 1);
print_r($people);
// Result:
Array
(
[0] => Jack
[1] => Humpty
[2] => Jill
)
|
insert_after_key(Array &$original, $value, $key)
The insert_after_key method adds an element to an array after the key specified
WARNING: The original array is edited by reference, only boolean success is returned
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$original |
必須 |
The array to use |
$value |
必須 |
The value(s) to insert |
$key |
必須 |
The key after which to insert |
|
返り値 |
boolean |
例 |
$people = array("Jack", "Jill");
Arr::insert_after_key($people, "Humpty", 1);
print_r($people);
// Result:
Array
(
[0] => Jack
[1] => Jill
[2] => Humpty
)
|
insert_after_value(Array &$original, $value, $search)
The insert_after_value method adds an element to an array after the value specified
WARNING: The original array is edited by reference, only boolean success is returned
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$original |
必須 |
The array to use |
$value |
必須 |
The value(s) to insert |
$search |
必須 |
The value after which to insert |
|
返り値 |
boolean |
例 |
$people = array("Jack", "Jill");
Arr::insert_after_value($people, "Humpty", "Jack");
print_r($people);
// Result:
Array
(
[0] => Jack
[1] => Humpty
[2] => Jill
)
|
sort($array, $key, $order = 'asc', $sort_flags = SORT_REGULAR)
The sort method sorts a multi-dimensional array by its values.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to sort |
$key |
必須 |
The key requested to sort by in $array. |
$order |
'asc'
|
The order (asc or desc). |
$sort_flags |
SORT_REGULAR
|
The php sort type flags (http://php.net/manual/en/function.sort.php) |
|
返り値 |
mixed |
例 |
$data = array(
array(
'info' => array(
'pet' => array(
'type' => 'dog'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'fish'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'cat'
)
),
),
);
$data = Arr::sort($data, 'info.pet.type');
// Result:
array(
array(
'info' => array(
'pet' => array(
'type' => 'cat'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'dog'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'fish'
)
),
),
);
|
multisort($array, $conditions, $ignore_case = false)
The sort method sorts a multi-dimensional array by multiple values.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$array |
必須 |
The array to sort |
$conditions |
必須 |
Array of sorting conditions. |
$ornire_case |
false
|
Wether to sort case insensitive. |
|
返り値 |
array |
例 |
$collection = array(
'i5' => array(
'name' => 'Carl',
'age' => 17,
'points' => 30,
'arr' => array(
'key' => 10,
),
),
'i7' => array(
'name' => 'carl',
'age' => 17,
'points' => 20,
'arr' => array(
'key' => 10,
),
),
'i2' => array(
'name' => 'Bert',
'age' => 20,
'points' => 30,
'arr' => array(
'key' => 10,
),
),
);
$collection = \Arr::multisort($collection, array(
'name' => SORT_ASC,
'points' => array(SORT_ASC, SORT_NUMERIC),
'age' => array(SORT_ASC, SORT_NUMERIC),
), true);
print_r($collection);
// Result
Array
(
[i2] => Array
(
[name] => Bert
[age] => 20
[points] => 30
[arr] => Array
(
[key] => 10
)
)
[i7] => Array
(
[name] => carl
[age] => 17
[points] => 20
[arr] => Array
(
[key] => 10
)
)
[i5] => Array
(
[name] => Carl
[age] => 17
[points] => 30
[arr] => Array
(
[key] => 10
)
)
)
|
in_array_recursive($needle, $haystack, $strict = false)
The in_array_recursive method checks wether a value is in an array recursively.
静的 |
はい |
パラメータ |
パラメータ |
規定値 |
説明 |
$needle |
必須 |
The value to search for |
$haystack |
必須 |
The array to search in. |
$strict |
false
|
The wether to use == or ===. |
|
返り値 |
bool |
例 |
$arr = array('one' => 1, 2, 3, array(56), 87);
echo Arr::in_array_recursive('56', $arr);
// Result: true
echo Arr::in_array_recursive('87', $arr, true);
// Result: false
echo Arr::in_array_recursive(87, $arr, true);
// Result: true
|
merge($array)
The merge method merges 2 arrays recursively, differs in 2 important ways from array_merge_recursive():
- When there's 2 different values and not both arrays, the latter value overwrites the earlier
instead of merging both into an array
- Numeric keys that don't conflict aren't changed, only when a numeric key already exists is the
value added using array_push()
静的 |
はい |
Parameters |
Param |
Type |
Default |
Description |
$array |
array |
required |
Multiple variables all of which must be arrays |
|
Returns |
array |
Throws |
InvalidArgumentException when one of the passed arguments is no array. |
Example |
$arr1 = array(
'one' => 1,
2,
3,
array(
56
),
87
);
$arr2 = array(
27,
90,
array(
'give_me' => 'bandwidth'
),
'90',
'php',
);
print_r( Arr::merge($arr1, $arr2) );
// Result:
Array
(
[one] => 1
[0] => 2
[1] => 3
[2] => Array (
[0] => 56
)
[3] => 87
[4] => 27
[5] => 90
[6] => Array (
[give_me] => bandwidth
)
[7] => 90
[8] => php
)
|
Procedural helpers
in_arrayi($needle, $haystack)
The in_arrayi function is a case-insensitive version of in_array.
パラメータ |
パラメータ |
型 |
規定値 |
説明 |
$needle |
string |
必須 |
the value to search for |
$haystrack |
array |
必須 |
the array to search in |
|
返り値 |
bool |
例 |
echo in_arrayi('This', array('something','tHis'));
// Result: true
echo in_arrayi('Thi', array('something','tHis'));
// Result: false
|