Passed
Push — master ( 6bf8e3...652baa )
by Stephen
01:48
created

array_map_assoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Returns a chunked array with calculated chunk size.
5
 *
6
 * @param array $array
7
 * @param int $min
8
 * @param int|null $max
9
 * @param bool $no_remainders
10
 * @param bool $preserve_keys
11
 * @return array
12
 */
13
function arrayChunks(array $array, $min = 0, $max = null, $no_remainders = false, $preserve_keys = true): array
14
{
15
    $chunks = array_chunk($array, chunkSizer(count($array), $min, $max), $preserve_keys);
0 ignored issues
show
Bug introduced by
It seems like $max can also be of type integer; however, parameter $max of chunkSizer() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

15
    $chunks = array_chunk($array, chunkSizer(count($array), $min, /** @scrutinizer ignore-type */ $max), $preserve_keys);
Loading history...
16
17
    // Check if the first chunk is the same length as the last chunk
18
    if ($no_remainders && count($chunks[0]) != count(array_reverse($chunks)[0])) {
19
        $remainder = array_pop($chunks);
20
        $last_chunk = array_pop($chunks);
21
22
        // Add the remainder chunk to the last equal sized chunk
23
        $chunks[] = array_merge($last_chunk, $remainder);
24
    }
25
26
    return $chunks;
27
}
28
29
/**
30
 * Flatten a multidimensional array into a 2D array without nested keys.
31
 *
32
 * @param array $array
33
 * @param bool $nest_keys
34
 * @return array
35
 */
36
function arrayFlattenKeys(array $array, $nest_keys = true): array
37
{
38
    $flat = [];
39
    foreach (array_keys($array) as $key) {
40
        if (is_array($array[$key])) {
41
            // If the key is an array, add each children keys to flattened array
42
            foreach ($array[$key] as $k => $v) {
43
                if ($nest_keys) {
44
                    $flat[$key.'_'.$k] = $v;
45
                } else {
46
                    $flat[$k] = $v;
47
                }
48
            }
49
        } else {
50
            $flat[$key] = $array[$key];
51
        }
52
    }
53
54
    return $flat;
55
}
56
57
/**
58
 * Remove particular keys from a multidimensional array.
59
 *
60
 * @param array $array
61
 * @param array|string $keys
62
 * @return array
63
 */
64
function arrayRemoveKeys(array $array, $keys): array
65
{
66
    $all_keys = array_keys($array);
67
    foreach ((array) $keys as $key) {
68
        if (in_array($key, $all_keys)) {
69
            unset($array[$key]);
70
        }
71
    }
72
73
    return $array;
74
}
75
76
/**
77
 * Sum the values of two arrays.
78
 *
79
 * @param array $array1
80
 * @param array $array2
81
 * @return array
82
 */
83
function sumArrays(array $array1, array $array2): array
84
{
85
    $array = [];
86
    foreach ($array1 as $index => $value) {
87
        $array[$index] = isset($array2[$index]) ? $array2[$index] + $value : $value;
88
    }
89
90
    return $array;
91
}
92
93
/**
94
 * Determine if all values in an array of key => value pairs are unique.
95
 *
96
 * @param array $array
97
 * @return bool
98
 */
99
function arrayValuesUnique(array $array): bool
100
{
101
    // Count the number of unique array values
102
    // Check to see if there is more than unique array_value
103
    return count(array_unique(array_values($array))) > 1;
104
}
105
106
/**
107
 * Determine if all array_values are equal to a certain value.
108
 *
109
 * @param array $array
110
 * @param mixed $value
111
 * @return bool
112
 */
113
function arrayValuesEqual(array $array, $value): bool
114
{
115
    // Check if all array values are equal to a certain value
116
    return count(array_keys($array, $value)) == count($array);
117
}
118
119
/**
120
 * Determine if an array is multidimensional and has keys.
121
 *
122
 * @param array $array
123
 * @return bool
124
 */
125
function arrayHasKeys(array $array): bool
126
{
127
    return count($array) == count($array, COUNT_RECURSIVE);
128
}
129
130
if (! function_exists('array_except')) {
131
    /**
132
     * Remove specific arrays of keys without modifying the original array.
133
     *
134
     * @param array $original
135
     * @param array $except
136
     * @return array
137
     */
138
    function array_except(array $original, array $except): array
139
    {
140
        return array_diff_key($original, array_flip((array) $except));
141
    }
142
}
143
144
/**
145
 * Return a best fit chunk size to be passed to array_chunks functions.
146
 *
147
 * Calculates the remainder of array sizes divided by the divisor
148
 * using modulus division.  Continues to calculate remainders until
149
 * the remainder is zero, signifying evenly sized chunks, or the
150
 * divisor is equal to the array size.  If a remainder of zero is not
151
 * found the lowest remainder is returned.
152
 *
153
 * @param int $array_size
154
 * @param int $min minimum chunk size
155
 * @param null $max maximum chunk size
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $max is correct as it would always require null to be passed?
Loading history...
156
 * @param int $divisor
157
 * @return int $remainder lowest calculated remainder
158
 */
159
function chunkSizer(int $array_size, $min = 0, $max = null, $divisor = 2): int
160
{
161
    // If the size of the array is a perfect square, return the square root
162
    if (gmp_perfect_square($array_size) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
163
        return sqrt($array_size);
164
    }
165
166
    // If min and max are the same return that value
167
    elseif ($min == $max) {
168
        return $min;
169
    }
170
171
    $max = (isset($max) ? $max : $array_size);
172
    $sizes = [];
173
    while ($divisor < $max) {
174
        $sizes[$divisor] = [
175
            // Number of chunks
176
            'rows'=> floor($array_size / $divisor),
177
178
            // Items in each chunk
179
            'cols' => $divisor,
180
181
            // Left over items in last chunk
182
            'remainder' => $array_size % $divisor,
183
        ];
184
        $divisor++;
185
    }
186
187
    // Filter sizes by column values
188
    return min(array_filter(array_column($sizes, 'cols', 'cols'), function ($size) use ($min, $max, $sizes) {
189
        return
190
            // Check that the remainder is no more than half of the number of columns
191
            ($sizes[$size]['remainder'] == 0 || $sizes[$size]['remainder'] >= $size / 2) &&
192
193
            // Check that the number of columns is greater than or equal than min and less than or equal than max
194
            $min <= $size && $size <= $max;
195
    }
196
    ));
197
}
198
199
/**
200
 * Only declare function if Illuminate/Collection is installed.
201
 */
202
if (function_exists('collect')) {
203
    /**
204
     * Return a flat array of values found in the $first array that are not found in the $second.
205
     *
206
     * @param array $first
207
     * @param array $second
208
     * @param bool $toArray
209
     * @return Illuminate\Support\Collection|array
210
     */
211
    function array_diff_flat(array $first, array $second, bool $toArray = true)
212
    {
213
        $collection = collect($first)
214
            ->diff($second)
215
            ->flatten();
216
217
        // Return as array
218
        if ($toArray) {
219
            return $collection->toArray();
220
        }
221
222
        // Return as Collection
223
        return $collection;
224
    }
225
}
226
227
/**
228
 * Remove a key from an array & return the key's value.
229
 *
230
 * @param array $array
231
 * @param string $key
232
 * @return mixed
233
 */
234
function arrayUnset(array $array, string $key)
235
{
236
    // Get the value
237
    try {
238
        $value = $array[$key];
239
    } catch (ErrorException $exception) {
240
        $value = null;
241
    }
242
243
    // Remove the value from the array
244
    unset($array[$key]);
245
246
    // Return the value
247
    return $value;
248
}
249
250
/**
251
 * Determine if all values in an array are null.
252
 *
253
 * @param array $array
254
 * @return bool
255
 */
256
function arrayValuesNull(array $array): bool
257
{
258
    return arrayValuesEqual($array, null);
259
}
260
261
/**
262
 * @deprecated
263
 *
264
 * Sum the values of two arrays
265
 *
266
 * @param $array1
267
 * @param $array2
268
 * @return array
269
 */
270
function sum_arrays(array $array1, array $array2): array
271
{
272
    return sumArrays($array1, $array2);
273
}
274
275
/**
276
 * Remove a key from an array & return the key's value.
277
 *
278
 * @param array $array
279
 * @param string $key
280
 * @return mixed
281
 * @deprecated
282
 */
283
function array_unset(array $array, string $key)
284
{
285
    return arrayUnset($array, $key);
286
}
287