Skip to content

Commit 9a11804

Browse files
authored
Merge pull request #8507 from kenjis/docs-array_helper.rst
docs: improve array_helper.rst
2 parents 1508db8 + 11d0066 commit 9a11804

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

user_guide_src/source/helpers/array_helper.rst

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Loading this Helper
1515
This helper is loaded using the following code:
1616

1717
.. literalinclude:: array_helper/001.php
18+
:lines: 2-
1819

1920
Available Functions
2021
===================
@@ -29,24 +30,28 @@ The following functions are available:
2930
:rtype: mixed
3031

3132
This method allows you to use dot-notation to search through an array for a specific-key,
32-
and allows the use of a the '*' wildcard. Given the following array:
33+
and allows the use of a the ``*`` wildcard. Given the following array:
3334

3435
.. literalinclude:: array_helper/002.php
36+
:lines: 2-
3537

36-
We can locate the value of 'fizz' by using the search string "foo.buzz.fizz". Likewise, the value
37-
of baz can be found with "foo.bar.baz":
38+
We can locate the value of ``fizz`` by using the search string ``foo.buzz.fizz``. Likewise, the value
39+
of ``baz`` can be found with ``foo.bar.baz``:
3840

3941
.. literalinclude:: array_helper/003.php
42+
:lines: 2-
4043

41-
You can use the asterisk as a wildcard to replace any of the segments. When found, it will search through all
44+
You can use the asterisk (``*``) as a wildcard to replace any of the segments. When found, it will search through all
4245
of the child nodes until it finds it. This is handy if you don't know the values, or if your values
4346
have a numeric index:
4447

4548
.. literalinclude:: array_helper/004.php
49+
:lines: 2-
4650

47-
If the array key contains a dot, then the key can be escaped with a backslash:
51+
If the array key contains a dot (``.``), then the key can be escaped with a backslash (``\``):
4852

4953
.. literalinclude:: array_helper/005.php
54+
:lines: 2-
5055

5156
.. note:: Prior to v4.2.0, ``dot_array_search('foo.bar.baz', ['foo' => ['bar' => 23]])`` returned ``23``
5257
due to a bug. v4.2.0 and later returns ``null``.
@@ -73,21 +78,24 @@ The following functions are available:
7378
from, e.g., the ``find()`` function of a model:
7479

7580
.. literalinclude:: array_helper/006.php
81+
:lines: 2-
7682

7783
Now sort this array by two keys. Note that the method supports the dot-notation
7884
to access values in deeper array levels, but does not support wildcards:
7985

8086
.. literalinclude:: array_helper/007.php
87+
:lines: 2-
8188

82-
The ``$players`` array is now sorted by the 'order' value in each players'
83-
'team' subarray. If this value is equal for several players, these players
84-
will be ordered by their 'position'. The resulting array is:
89+
The ``$players`` array is now sorted by the ``order`` value in each players'
90+
``team`` subarray. If this value is equal for several players, these players
91+
will be ordered by their ``position``. The resulting array is:
8592

8693
.. literalinclude:: array_helper/008.php
94+
:lines: 2-
8795

8896
In the same way, the method can also handle an array of objects. In the example
89-
above it is further possible that each 'player' is represented by an array,
90-
while the 'teams' are objects. The method will detect the type of elements in
97+
above it is further possible that each ``player`` is represented by an array,
98+
while the ``teams`` are objects. The method will detect the type of elements in
9199
each nesting level and handle it accordingly.
92100

93101
.. php:function:: array_flatten_with_dots(iterable $array[, string $id = '']): array
@@ -101,16 +109,19 @@ The following functions are available:
101109
as separators for the keys.
102110

103111
.. literalinclude:: array_helper/009.php
112+
:lines: 2-
104113

105114
On inspection, ``$flattened`` is equal to:
106115

107116
.. literalinclude:: array_helper/010.php
117+
:lines: 2-
108118

109119
Users may use the ``$id`` parameter on their own, but are not required to do so.
110120
The function uses this parameter internally to track the flattened keys. If users
111121
will be supplying an initial ``$id``, it will be prepended to all keys.
112122

113123
.. literalinclude:: array_helper/011.php
124+
:lines: 2-
114125

115126
.. php:function:: array_group_by(array $array, array $indexes[, bool $includeEmpty = false]): array
116127
@@ -126,12 +137,15 @@ The following functions are available:
126137
The example shows some data (i.e. loaded from an API) with nested arrays.
127138

128139
.. literalinclude:: array_helper/012.php
129-
130-
We want to group them first by "gender", then by "hr.department" (max depth = 2).
140+
:lines: 2-
141+
142+
We want to group them first by ``gender``, then by ``hr.department`` (max depth = 2).
131143
First the result when excluding empty values:
132144

133145
.. literalinclude:: array_helper/013.php
134-
146+
:lines: 2-
147+
135148
And here the same code, but this time we want to include empty values:
136149

137150
.. literalinclude:: array_helper/014.php
151+
:lines: 2-

0 commit comments

Comments
 (0)