This repository has been archived by the owner on Nov 16, 2022. It is now read-only.
forked from nette/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.texy
132 lines (85 loc) · 4.01 KB
/
arrays.texy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
Array Processing - Nette\Utils\Arrays
*************************************
.[perex]
[Nette\Utils\Arrays |api:] is a static class, which contains a handful of handy array functions.
All examples assume the following class alias is defined:
/--php
use Nette\Utils\Arrays;
\--
get($array, $key, $default=NULL) .{toc: get()}
--------------------------------------------
Returns `$array[$key]` item. If it does not exist, `Nette\InvalidArgumentException` is thrown, unless a default return value is set as third argument.
/--php
// if $array['foo'] does not exist, throws an exception
$value = Arrays::get($array, 'foo');
// if $array['foo'] does not exist, returns 'bar'
$value = Arrays::get($array, 'foo', 'bar');
\--
Argument `$key` may as well be an array.
/--php
$array = array('color' => array('favorite' => 'red'), 5);
$value = Arrays::get($array, array('color', 'favorite'));
// returns 'red'
\--
getRef(&$array, $key) .{toc: getRef()}
------------------------------------
Gets reference to given `$array[$key]`. If the index does not exist, new one is created with `NULL`.
/--php
$valueRef = & Arrays::getRef($array, 'foo');
// returns $array['foo'] reference
\--
Works with multidimensional arrays as well as [get() | #get()].
/--php
$value = & Arrays::get($array, array('color', 'favorite'));
// returns $array['color']['favorite'] reference
\--
grep($array, $pattern, $flags=NULL) .{toc: grep()}
------------------------------------------------
Returns only those array items, which matches a regular expression `$pattern`. Regex compilation or runtime error throw `Nette\RegexpException`.
/--php
$filteredArray = Arrays::grep($array, '~^\d+$~');
// returns only numerical items
\--
Value `PREG_GREP_INVERT` may be set as `$flags`, which inverts the selection.
searchKey($array, $key) .{toc: searchKey()}
-----------------------------------------
Returns zero-indexed position of given array key. Returns `FALSE` if key is not found.
/--php
$array = array('first' => 10, 'second' => 20);
$position = Arrays::searchKey($array, 'first'); // returns 0
\--
insertAfter(&$array, $key, $inserted) .{toc: insertAfter()}
---------------------------------------------------------
Appends array `$inserted` after item with `$key` index. If such a `$key` does not exist, the array is inserted at the end.
/--php
$array = array('first' => 10, 'second' => 20);
Arrays::insertAfter($array, 'first', array('hello' => 'world'));
// $array = array('first' => 10, 'hello' => 'world', 'second' => 20);
\--
insertBefore(&$array, $key, $inserted) .{toc: insertBefore()}
-----------------------------------------------------------
Prepends content of `$inserted` array into `$array` before item with `$key` index. If such a `$key` does not exist, the array is inserted at the beginning.
/--php
$array = array('first' => 10, 'second' => 20);
Arrays::insertBefore($array, 'first', array('hello' => 'world'));
// $array = array('hello' => 'world', 'first' => 10, 'second' => 20);
\--
mergeTree($array1, $array2) .{toc: mergeTree()}
-------------------------------------------
Merges two arrays recursively. Useful for combining tree structures. It behaves as the `+` operator applied to arrays, ie. it adds to keys/values of the second array to the the first one. In case of collision, values of first array are used.
/--php
$array1 = array('color' => array('favorite' => 'red'), 5);
$array2 = array(10, 'color' => array('favorite' => 'green', 'blue'));
$array = Arrays::mergeTree($array1, $array2);
// $array = array('color' => array('favorite' => 'red', 'blue'), 5);
\--
Values from second array are always appended to the first. Though the disappearance of value `10` might be confusing, it's fine - both `5` of the first array and the `10` do have same key `0`.
renameKey(&$array, $oldKey, $newKey) .{toc: renameKey()}
------------------------------------------------------
Renames a key.
/--php
$array = array('first' => 10, 'second' => 20);
Arrays::renameKey($array, 'first', 'renamed');
// $array = array('renamed' => 10, 'second' => 20);
\--
{{composer: nette/utils}}