1 | <?php |
||
6 | class ArrayHelper |
||
7 | { |
||
8 | /** |
||
9 | * Retrieves the value of an array element or object property with the given key or property name. |
||
10 | * If the key does not exist in the array or object, the default value will be returned instead. |
||
11 | * |
||
12 | * The key may be specified in a dot format to retrieve the value of a sub-array or the property |
||
13 | * of an embedded object. In particular, if the key is `x.y.z`, then the returned value would |
||
14 | * be `$array['x']['y']['z']` or `$array->x->y->z` (if `$array` is an object). If `$array['x']` |
||
15 | * or `$array->x` is neither an array nor an object, the default value will be returned. |
||
16 | * Note that if the array already has an element `x.y.z`, then its value will be returned |
||
17 | * instead of going through the sub-arrays. So it is better to be done specifying an array of key names |
||
18 | * like `['x', 'y', 'z']`. |
||
19 | * |
||
20 | * Below are some usage examples, |
||
21 | * |
||
22 | * ~~~ |
||
23 | * // working with array |
||
24 | * $username = ArrayHelper::getValue($_POST, 'username'); |
||
25 | * // working with object |
||
26 | * $username = ArrayHelper::getValue($user, 'username'); |
||
27 | * // working with anonymous function |
||
28 | * $fullName = ArrayHelper::getValue($user, function ($user, $defaultValue) { |
||
29 | * return $user->firstName . ' ' . $user->lastName; |
||
30 | * }); |
||
31 | * // using dot format to retrieve the property of embedded object |
||
32 | * $street = ArrayHelper::getValue($users, 'address.street'); |
||
33 | * // using an array of keys to retrieve the value |
||
34 | * $value = ArrayHelper::getValue($versions, ['1.0', 'date']); |
||
35 | * ~~~ |
||
36 | * |
||
37 | * @param array|object $array array or object to extract value from |
||
38 | * @param string|\Closure|array $key key name of the array element, an array of keys or property name of the object, |
||
39 | * or an anonymous function returning the value. The anonymous function signature should be: |
||
40 | * `function($array, $defaultValue)`. |
||
41 | * @param mixed $default the default value to be returned if the specified array key does not exist. Not used when |
||
42 | * getting value from an object. |
||
43 | * |
||
44 | * @return mixed the value of the element if found, default value otherwise |
||
45 | */ |
||
46 | public static function getValue($array, $key, $default = null) |
||
73 | |||
74 | /** |
||
75 | * Removes an item from an array and returns the value. If the key does not exist in the array, the default value |
||
76 | * will be returned instead. |
||
77 | * |
||
78 | * Usage examples, |
||
79 | * |
||
80 | * ~~~ |
||
81 | * // $array = ['type' => 'A', 'options' => [1, 2]]; |
||
82 | * // working with array |
||
83 | * $type = ArrayHelper::remove($array, 'type'); |
||
84 | * // $array content |
||
85 | * // $array = ['options' => [1, 2]]; |
||
86 | * ~~~ |
||
87 | * |
||
88 | * @param array $array the array to extract value from |
||
89 | * @param string $key key name of the array element |
||
90 | * @param mixed $default the default value to be returned if the specified key does not exist |
||
91 | * |
||
92 | * @return mixed|null the value of the element if found, default value otherwise |
||
93 | */ |
||
94 | public static function remove(&$array, $key, $default = null) |
||
105 | } |
||
106 |