GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 966fe3...40d637 )
by t
04:58 queued 02:23
created

Arrays::values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Class Arrays
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use icy2003\php\I;
13
use LogicException;
14
15
/**
16
 * 数组类
17
 *
18
 * 常见数组格式的拼装和处理
19
 *
20
 * @test icy2003\php_tests\ihelpers\ArraysTest
21
 */
22
class Arrays
23
{
24
25
    /**
26
     * 以各个元素的某字段值作为键重新指回该元素,此值对于该元素需唯一
27
     *
28
     * @param array  $array
29
     * @param string $index 用来作为键的某字段
30
     * @param boolean $isMerge 是否合并相同键的项到数组,默认否(也就是后者覆盖前者)
31
     *
32
     * @return array
33
     *
34
     * @tested
35
     */
36 1
    public static function indexBy($array, $index, $isMerge = false)
37
    {
38 1
        $result = [];
39 1
        foreach ($array as $row) {
40 1
            if (!array_key_exists($index, $row)) {
41 1
                return [];
42
            }
43 1
            if (false === $isMerge) {
44 1
                $result[$row[$index]] = $row;
45
            } else {
46 1
                $result[$row[$index]][] = $row;
47
            }
48
        }
49
50 1
        return $result;
51
    }
52
53
    /**
54
     * 选取数组中指定键的某几列
55
     *
56
     * - 简单理解就是:从数据库里查出来几条数据,只拿其中的几个属性
57
     * - 当 $dimension 为 2 时,理解为从几条数据里拿属性
58
     * - 当 $dimension 为 1 时,理解为从一条数据里拿属性
59
     *
60
     * @param array $array
61
     * @param array $keys 某几项字段,支持 I::get 的键格式,如果给 null,则返回原数组
62
     * @param integer $dimension 维度,只能为 1 或 2,默认 2,表示处理二维数组
63
     *
64
     * @return array
65
     *
66
     * @tested
67
     */
68 1
    public static function columns($array, $keys = null, $dimension = 2)
69
    {
70 1
        if (null === $keys) {
71 1
            return $array;
72
        }
73 1
        $result = [];
74 1
        if (2 === $dimension) {
75 1
            foreach ($array as $k => $row) {
76 1
                foreach ($keys as $key) {
77 1
                    if (array_key_exists($key, $row)) {
78 1
                        $result[$k][$key] = I::get($row, $key);
79
                    } else {
80 1
                        $result[$k][$key] = null;
81
                    }
82
                }
83
            }
84
        }
85 1
        if (1 === $dimension) {
86 1
            foreach ($array as $k => $row) {
87 1
                if (in_array($k, $keys, true)) {
88 1
                    $result[$k] = $row;
89
                } else {
90 1
                    $result[$k] = null;
91
                }
92
            }
93
        }
94
95 1
        return $result;
96
    }
97
98
    /**
99
     * 返回二维(或者更高)数组中指定键的一列的所有值
100
     *
101
     * - array_column 要求 PHP >= 5.5,这个是兼容 5.5 以下的
102
     * - 如果需要取某几项,使用 Arrays::columns
103
     * - 简单理解就是:从数据库里查出来几条数据,只要其中某个属性的所有值
104
     * - USE_CUSTOM
105
     *
106
     * @see http://php.net/array_column
107
     *
108
     * @param array $array
109
     * @param string $column 需要被取出来的字段
110
     * @param string $index 作为 index 的字段
111
     *
112
     * @return array
113
     *
114
     * @tested
115
     */
116 1
    public static function column($array, $column, $index = null)
117
    {
118 1
        if (function_exists('array_column') && false === I::ini('USE_CUSTOM')) {
119 1
            return array_column($array, $column, $index);
120
        } else {
121 1
            $result = [];
122 1
            foreach ($array as $row) {
123 1
                $data = I::get($row, $column);
124 1
                if (null === $index) {
125 1
                    $result[] = $data;
126
                } else {
127 1
                    $result[$row[$index]] = $data;
128
                }
129
            }
130
131 1
            return $result;
132
        }
133
    }
134
135
    /**
136
     * 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值
137
     *
138
     * - array_combine:两个数组元素个数不一致将报错
139
     * - 在两个数组元素个数不一致时,以键为准:
140
     *      1.键比值多,值都被填充为 null
141
     *      2.值比键多,值被舍去
142
     * ```
143
     *
144
     * @see http://php.net/array_combine
145
     *
146
     * @param array $keys
147
     * @param array $values
148
     *
149
     * @return array
150
     *
151
     * @tested
152
     */
153 2
    public static function combine($keys, $values)
154
    {
155 2
        if (count($keys) == count($values)) {
156 2
            return (array) array_combine($keys, $values);
157
        }
158 1
        $array = [];
159 1
        foreach ($keys as $index => $key) {
160 1
            $array[$key] = I::get($values, $index);
161
        }
162 1
        return $array;
163
    }
164
165
    /**
166
     * 递归地合并多个数组
167
     *
168
     * - array_merge_recursive:如果有相同的键,后者会覆盖前者
169
     * - 此函数会合并两个相同键的值到一个数组里
170
     *
171
     * @see http://php.net/array_merge_recursive
172
     *
173
     * @param array $a 数组1
174
     * @param array $b 数组2(可以任意个数组)
175
     *
176
     * @return array
177
     *
178
     * @tested
179
     */
180 2
    public static function merge($a, $b)
181
    {
182 2
        $args = func_get_args();
183 2
        $res = array_shift($args);
184 2
        while (!empty($args)) {
185 2
            foreach (array_shift($args) as $k => $v) {
186 2
                if (is_int($k)) {
187 2
                    if (array_key_exists($k, $res)) {
188 2
                        $res[] = $v;
189
                    } else {
190 2
                        $res[$k] = $v;
191
                    }
192 1
                } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
193 1
                    $res[$k] = self::merge($res[$k], $v);
194
                } else {
195 1
                    $res[$k] = $v;
196
                }
197
            }
198
        }
199
200 2
        return $res;
201
    }
202
203
    /**
204
     *  range 的性能优化版
205
     *
206
     * @see http://php.net/manual/zh/language.generators.overview.php
207
     * @version PHP >= 5.5
208
     *
209
     * @param integer $start 开始
210
     * @param integer $end 结束
211
     * @param integer $step 步长
212
     *
213
     * @return \Generator
214
     * @throws \LogicException
215
     *
216
     * @tested
217
     */
218 3
    public static function rangeGenerator($start, $end, $step = 1)
219
    {
220 3
        if ($start < $end) {
221 2
            if ($step <= 0) {
222 1
                throw new LogicException('步长必须大于 0');
223
            }
224 2
            for ($i = $start; $i <= $end; $i += $step) {
225 2
                yield $i;
226
            }
227 2
        } elseif ($start > $end) {
228 1
            if ($step >= 0) {
229 1
                throw new LogicException('步长必须小于 0');
230
            }
231 1
            for ($i = $start; $i >= $end; $i += $step) {
232 1
                yield $i;
233
            }
234
        } else {
235 1
            yield $start;
236
        }
237 3
    }
238
239
    /**
240
     * 找到符合条件的第一项
241
     *
242
     * @param array $array
243
     * @param callback $callback 条件回调,结果为 true 的第一项会被取出
244
     *
245
     * @return mixed
246
     *
247
     * @tested
248
     */
249 1
    public static function detectFirst($array, $callback)
250
    {
251 1
        foreach ($array as $key => $item) {
252 1
            if (true === I::trigger($callback, [$item, $key])) {
253 1
                return $item;
254
            }
255
        }
256 1
        return null;
257
    }
258
259
    /**
260
     * 找到符合条件的所有项
261
     *
262
     * @param array $array
263
     * @param callback $callback 条件回调,结果为 true 的所有项会被取出
264
     * @param callback $filter 对符合条件的项进行回调处理并返回
265
     *
266
     * @return array
267
     *
268
     * @tested
269
     */
270 1
    public static function detectAll($array, $callback, $filter = null)
271
    {
272 1
        $all = [];
273 1
        foreach ($array as $key => $item) {
274 1
            if (true === I::trigger($callback, [$item, $key])) {
275 1
                if (null !== $filter) {
276 1
                    $all[$key] = I::trigger($filter, [$item, $key]);
277
                } else {
278 1
                    $all[$key] = $item;
279
                }
280
            }
281
        }
282 1
        return $all;
283
    }
284
285
    /**
286
     * 返回数组的最后一个元素的键
287
     *
288
     * - array_key_last:需要 PHP7.3.0+ 才能支持
289
     * - USE_CUSTOM
290
     *
291
     * @param array $array
292
     *
293
     * @return string|null
294
     *
295
     * @tested
296
     */
297 1
    public static function keyLast($array)
298
    {
299 1
        if (!is_array($array) || empty($array)) {
300 1
            return null;
301
        }
302 1
        if (function_exists('array_key_last') && false === I::ini('USE_CUSTOM')) {
303 1
            return array_key_last($array);
304
        }
305 1
        end($array);
306 1
        return key($array);
307
    }
308
309
    /**
310
     * 返回数组的第一个元素的键
311
     *
312
     * - array_key_first:需要 PHP7.3.0+ 才能支持
313
     * - USE_CUSTOM
314
     *
315
     * @param array $array
316
     *
317
     * @return string|null
318
     *
319
     * @tested
320
     */
321 1
    public static function keyFirst($array)
322
    {
323 1
        if (!is_array($array) || empty($array)) {
324 1
            return null;
325
        }
326 1
        if (function_exists('array_key_first') && false === I::ini('USE_CUSTOM')) {
327 1
            return array_key_first($array);
328
        }
329 1
        reset($array);
330 1
        return key($array);
331
    }
332
333
    /**
334
     * 获取数组的维度
335
     *
336
     * @param array $array 多维数组
337
     *
338
     * @return integer
339
     *
340
     * @tested
341
     */
342 1
    public static function dimension($array)
343
    {
344 1
        if (!is_array($array)) {
345 1
            return 0;
346
        }
347 1
        $max = 1;
348 1
        foreach ($array as $value) {
349 1
            if (is_array($value)) {
350 1
                $d = self::dimension($value) + 1;
351 1
                if ($d > $max) {
352 1
                    $max = $d;
353
                }
354
            }
355
        }
356 1
        return $max;
357
    }
358
359
    /**
360
     * 判断数组是不是关联数组
361
     *
362
     * @param array $array
363
     *
364
     * @return boolean
365
     *
366
     * @tested
367
     */
368 1
    public static function isAssoc($array)
369
    {
370 1
        if (is_array($array)) {
371 1
            $keys = array_keys($array);
372 1
            return $keys !== array_keys($keys);
373
        }
374 1
        return false;
375
    }
376
377
    /**
378
     * 判断数组是不是索引数组
379
     *
380
     * 索引数组必须是下标从 0 开始的数组,键是数字还是字符串类型的数字无所谓
381
     *
382
     * @param array $array
383
     *
384
     * @return boolean
385
     *
386
     * @tested
387
     */
388 1
    public static function isIndexed($array)
389
    {
390 1
        if (is_array($array)) {
391 1
            $keys = array_keys($array);
392 1
            return $keys === array_keys($keys);
393
        }
394 1
        return false;
395
    }
396
397
    /**
398
     * 返回数组的顺数第 n 个元素,其中 n >= 1 且为整数,空数组直接返回 null
399
     *
400
     * - 支持关联数组,超过数组长度会对数组长度求余后查找
401
     *
402
     * @param array $array
403
     * @param int $pos 顺数第 n 个,默认 1
404
     *
405
     * @return mixed
406
     *
407
     * @tested
408
     */
409 2
    public static function first($array, $pos = 1)
410
    {
411 2
        if (0 === ($count = self::count($array))) {
412 1
            return null;
413
        }
414 2
        $p = $pos % $count;
415 2
        if (0 === $p) {
416 1
            $p = $count;
417
        }
418 2
        for ($i = 1; $i < $p; $i++) {
419 1
            next($array);
420
        }
421 2
        return current($array);
422
    }
423
424
    /**
425
     * 返回数组的倒数第 n 个元素,其中 n >= 1 且为整数,空数组直接返回 null
426
     *
427
     * - 支持关联数组,超过数组长度会对数组长度求余后查找
428
     *
429
     * @param array $array
430
     * @param int $pos 倒数第 n 个,默认 1
431
     *
432
     * @return mixed
433
     *
434
     * @tested
435
     */
436 1
    public static function last($array, $pos = 1)
437
    {
438 1
        if (0 === ($count = self::count($array))) {
439 1
            return null;
440
        }
441 1
        $p = $pos % $count;
442 1
        if (0 === $p) {
443 1
            $p = $count;
444
        }
445 1
        end($array);
446 1
        for ($i = 1; $i < $p; $i++) {
447 1
            prev($array);
448
        }
449 1
        return current($array);
450
    }
451
452
    /**
453
     * 计算数组中的单元数目
454
     *
455
     * - count:在非数组情况下,除了 null 会返回 0,其他都返回 1,囧
456
     * - $callback 参数用于对符合条件的项做筛选
457
     *
458
     * @param array $array 数组
459
     * @param callback|mixed $callback 回调,返回回调值为 true 的项,如果此参数是非回调类型,表示查询和此值严格相等的项
460
     * @param boolean $isStrict 是否为严格模式,如果为 false,回调值为 true 值的也会返回,为字符串时不使用严格比较
461
     *
462
     * @return integer
463
     *
464
     * @tested
465
     */
466 5
    public static function count($array, $callback = null, $isStrict = true)
467
    {
468 5
        $count = 0;
469 5
        if (is_array($array)) {
0 ignored issues
show
introduced by
The condition is_array($array) is always true.
Loading history...
470 5
            if (null === $callback) {
471 5
                return count($array);
472
            } else {
473 1
                $function = $callback;
474 1
                if (false === is_callable($callback)) {
475
                    $function = function ($row) use ($callback, $isStrict) {
476 1
                        return true === $isStrict ? $row === $callback : $row == $callback;
477 1
                    };
478
                }
479 1
                foreach ($array as $key => $row) {
480 1
                    if (true === I::trigger($function, [$row, $key])) {
481 1
                        $count++;
482
                    }
483
                }
484
485
            }
486
        }
487 1
        return $count;
488
    }
489
490
    /**
491
     * 返回指定长度的数组,不足的值设置为 null
492
     *
493
     * @param array $array
494
     * @param integer $count 指定长度
495
     * @param callback $callback 回调参数:数组的值、数组的键
496
     *
497
     * @return array
498
     *
499
     * @tested
500
     */
501 1
    public static function lists($array, $count = null, $callback = null)
502
    {
503 1
        null === $count && $count = self::count($array);
504 1
        $arrayCount = self::count($array);
505 1
        if ($arrayCount >= $count) {
506 1
            $return = $array;
507
        } else {
508 1
            $return = self::merge($array, self::fill(0, $count - $arrayCount, null));
509
        }
510 1
        if (null !== $callback) {
511 1
            foreach ($return as $key => $value) {
512 1
                $return[$key] = I::trigger($callback, [$value, $key]);
513
            }
514
        }
515 1
        return $return;
516
    }
517
518
    /**
519
     * 获取指定某些键的项的值
520
     *
521
     * @param array $array
522
     * @param array|string $keys 数组或逗号字符串
523
     *
524
     * @return array
525
     *
526
     * @tested
527
     */
528 1
    public static function values($array, $keys = null)
529
    {
530 1
        return array_values(self::some($array, $keys));
531
    }
532
533
    /**
534
     * 获取指定某些键的项
535
     *
536
     * @param array $array
537
     * @param array|string $keys 数组或都好字符串
538
     *
539
     * @return array
540
     *
541
     * @tested
542
     */
543 2
    public static function some($array, $keys = null)
544
    {
545 2
        if (null === $keys) {
546 1
            return $array;
547
        }
548 2
        $keys = Strings::toArray($keys);
549 2
        return array_intersect_key($array, array_flip($keys));
550
    }
551
552
    /**
553
     * 获取指定除了某些键的项
554
     *
555
     * @param array $array
556
     * @param array|string $keys
557
     *
558
     * @return array
559
     *
560
     * @tested
561
     */
562 1
    public static function exceptedKeys($array, $keys)
563
    {
564 1
        $keys = Strings::toArray($keys);
565 1
        return array_diff_key($array, array_flip($keys));
566
    }
567
568
    /**
569
     * 检查数组里是否有指定的所有键名或索引
570
     *
571
     * - array_key_exists:检测一个指定的键
572
     * - Arrays::keyExistsOne:检测数组里是否存在指定的某些键
573
     *
574
     * @param array $keys 要检查的键
575
     * @param array $array
576
     * @param array $diff 引用返回不包含的键
577
     *
578
     * @return boolean
579
     *
580
     * @tested
581
     */
582 1
    public static function keyExistsAll($keys, $array, &$diff = null)
583
    {
584
585 1
        return I::isEmpty($diff = array_diff($keys, array_keys($array)));
586
    }
587
588
    /**
589
     * 检查数组里是否有指定的某些键名或索引
590
     *
591
     * @param array $keys 要检查的键
592
     * @param array $array
593
     * @param array $find 引用返回包含的键
594
     *
595
     * @return boolean
596
     *
597
     * @tested
598
     */
599 1
    public static function keyExistsSome($keys, $array, &$find = null)
600
    {
601 1
        return !I::isEmpty($find = array_intersect($keys, array_keys($array)));
602
    }
603
604
    /**
605
     * 检查数组里是否有指定的所有值
606
     *
607
     * @param array $values 要检查的值
608
     * @param array $array
609
     * @param array $diff 引用返回不包含的值
610
     *
611
     * @return boolean
612
     *
613
     * @tested
614
     */
615 1
    public static function valueExistsAll($values, $array, &$diff = null)
616
    {
617 1
        return I::isEmpty($diff = array_diff($values, array_values($array)));
618
    }
619
620
    /**
621
     * 检查数组里是否有指定的某些值
622
     *
623
     * @param array $values 要检查的值
624
     * @param array $array
625
     * @param array $find 引用返回包含的值
626
     *
627
     * @return boolean
628
     *
629
     * @tested
630
     */
631 1
    public static function valueExistsSome($values, $array, &$find = null)
632
    {
633 1
        return !I::isEmpty($find = array_intersect($values, array_values($array)));
634
    }
635
636
    /**
637
     * 参照 PHP 的 array_combine 函数,array_combine 得到的是一行记录的格式,该函数得到多行
638
     *
639
     * - arrays 里的每个数组会和 keys 使用 self::combine 合并,最终合并成为一个二维数组
640
     *
641
     * @param array $keys 作为键的字段
642
     * @param array $arrays
643
     *
644
     * @return array
645
     *
646
     * @tested
647
     */
648 1
    public static function combines($keys, $arrays)
649
    {
650 1
        $result = [];
651 1
        foreach ($arrays as $k => $array) {
652 1
            $result[$k] = self::combine($keys, $array);
653
        }
654
655 1
        return $result;
656
    }
657
658
    /**
659
     * 把数组里逗号字符串拆分,并且去掉重复的部分
660
     *
661
     * @param array $array
662
     *
663
     * @return array
664
     *
665
     * @tested
666
     */
667 1
    public static function toPart($array)
668
    {
669 1
        return array_values(
670 1
            array_filter(
671 1
                array_keys(
672 1
                    array_flip(
673 1
                        explode(',', implode(',', $array))
674
                    )
675
                )
676
            )
677
        );
678
    }
679
680
    /**
681
     * 矩阵转置
682
     *
683
     * @param array $array 待转置的矩阵
684
     *
685
     * @return array
686
     *
687
     * @tested
688
     */
689 1
    public static function transposed($array)
690
    {
691 1
        $data = [];
692 1
        foreach ($array as $r => $row) {
693 1
            foreach ($row as $c => $col) {
694 1
                $data[$c][$r] = $col;
695
            }
696
        }
697 1
        return $data;
698
    }
699
700
    /**
701
     * 普通二维数组转化成 Excel 单元格二维数组
702
     *
703
     * @param array $array
704
     *
705
     * @return array
706
     *
707
     * @tested
708
     */
709 1
    public static function toCellArray($array)
710
    {
711 1
        $data = [];
712 1
        $rowIndex = 0;
713 1
        foreach ($array as $row) {
714 1
            $rowIndex++;
715 1
            $colIndex = 'A';
716 1
            foreach ($row as $col) {
717 1
                $data[$rowIndex][$colIndex++] = $col;
718
            }
719
        }
720 1
        return $data;
721
    }
722
723
    /**
724
     * 返回矩阵的列数和行数
725
     *
726
     * - 返回两个元素的一维数组,第一个元素表示矩阵的列数,第二个元素表示矩阵的行数
727
     *
728
     * @param array $array
729
     *
730
     * @return array
731
     *
732
     * @tested
733
     */
734 1
    public static function colRowCount($array)
735
    {
736 1
        return [self::count(self::first($array)), self::count($array)];
737
    }
738
739
    /**
740
     * 用给定的值填充数组
741
     *
742
     * - array_fill:第一参数在为负的时候,生成的数组的第二个元素是从 0 开始的!
743
     *
744
     * @param int $startIndex 返回的数组的第一个索引值
745
     * @param int $num 插入元素的数量。如果为 0 或者负数,则返回空数组
746
     * @param mixed $value 用来填充的值
747
     *
748
     * @return array
749
     *
750
     * @tested
751
     */
752 2
    public static function fill($startIndex, $num, $value)
753
    {
754 2
        if ($num <= 0) {
755 1
            return [];
756
        }
757 2
        $array = [];
758 2
        foreach (self::rangeGenerator($startIndex, $startIndex + $num - 1) as $key) {
759 2
            $array[$key] = $value;
760
        }
761 2
        return $array;
762
    }
763
764
    /**
765
     * 让 var_export 返回 `[]` 的格式
766
     *
767
     * @param mixed $expression 变量
768
     * @param bool $return 默认值 为 true,即返回字符串而不是输出
769
     *
770
     * @return mixed
771
     *
772
     * @tested
773
     */
774 1
    public static function export($expression, $return = true)
775
    {
776 1
        $export = var_export($expression, true);
777 1
        $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export);
778 1
        $array = preg_split("/\r\n|\n|\r/", $export);
779 1
        $array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array);
780 1
        $export = implode(PHP_EOL, array_filter(["["] + $array));
781 1
        if (true === $return) {
782 1
            return $export;
783
        } else {
784 1
            echo $export;
785
        }
786 1
    }
787
788
    /**
789
     * 将 CSV 文本转成数组
790
     *
791
     * @param string $csvString
792
     *
793
     * @return array
794
     *
795
     * @tested
796
     */
797 1
    public static function fromCsv($csvString)
798
    {
799 1
        $lines = explode(PHP_EOL, $csvString);
800 1
        $array = [];
801 1
        foreach ($lines as $line) {
802 1
            $array[] = explode(',', $line);
803
        }
804 1
        return $array;
805
    }
806
807
    /**
808
     * 在数组中搜索给定的值,如果成功则返回首个相应的键名
809
     *
810
     * - 第一参数如果不是回调函数,则此方法等同于 array_search
811
     * - 第一参数如果是回调函数,则找到的条件为:回调值为 true
812
     * - 第三参数如果是 false,则回调值只需要 true 值即可(例如:1)
813
     *
814
     * @param mixed|callback $search 搜索的值
815
     * @param array $array 这个数组
816
     * @param boolean $isStrict 是否检查完全相同的元素
817
     *
818
     * @return mixed|false
819
     *
820
     * @tested
821
     */
822 1
    public static function search($search, $array, $isStrict = false)
823
    {
824 1
        if (false === is_callable($search)) {
825 1
            return array_search($search, $array, $isStrict);
826
        }
827 1
        foreach ($array as $key => $row) {
828 1
            $result = I::trigger($search, [$row]);
829 1
            if (true === $isStrict && true === $result || false === $isStrict && true == $result) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (true === $isStrict && t...rict && true == $result, Probably Intended Meaning: true === $isStrict && (t...ict && true == $result)
Loading history...
830 1
                return $key;
831
            }
832
        }
833 1
        return false;
834
    }
835
836
    /**
837
     * 递增数组的一个值并返回
838
     *
839
     * - 如果该值不存在,则默认为 0
840
     *
841
     * @param array $array 引用返回数组
842
     * @param string $key
843
     * @param integer $step 步长,默认 1
844
     *
845
     * @return double|integer
846
     *
847
     * @tested
848
     */
849 1
    public static function increment(&$array, $key, $step = 1)
850
    {
851 1
        $array[$key] = I::get($array, $key, 0) + $step;
852 1
        return $array[$key];
853
    }
854
855
    /**
856
     * 递减数组的一个值并返回
857
     *
858
     * - 如果该值不存在,则默认为 0
859
     *
860
     * @param array $array 引用返回数组
861
     * @param string $key
862
     * @param integer $step 步长,默认 1
863
     *
864
     * @return double|integer
865
     *
866
     * @tested
867
     */
868 1
    public static function decrement(&$array, $key, $step = 1)
869
    {
870 1
        $array[$key] = I::get($array, $key, 0) - $step;
871 1
        return $array[$key];
872
    }
873
874
}
875