• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-encore-bundle / 4498491298

pending completion
4498491298

push

github

GitHub
Reduce utils bundle usage (#28)

18 of 18 new or added lines in 4 files covered. (100.0%)

618 of 817 relevant lines covered (75.64%)

1.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/Helper/ArrayHelper.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\EncoreBundle\Helper;
10

11
class ArrayHelper
12
{
13
    /**
14
     * Remove duplicate values from multi-dimensional arrays based on a given key.
15
     *
16
     * @param $array
17
     * @param $key
18
     *
19
     * @return array
20
     */
21
    public static function arrayUniqueMultidimensional($array, $key, bool $flipOrder = false)
22
    {
23
        $temp_array = [];
16✔
24
        $i = 0;
16✔
25
        $key_array = [];
16✔
26

27
        if ($flipOrder) {
16✔
28
            $array = array_reverse($array);
15✔
29
        }
30

31
        foreach ($array as $val) {
16✔
32
            if (!\in_array($val[$key], $key_array, true)) {
15✔
33
                $key_array[$i] = $val[$key];
15✔
34
                $temp_array[$i] = $val;
15✔
35
            }
36
            ++$i;
15✔
37
        }
38
        if ($flipOrder) {
16✔
39
            $temp_array = array_reverse($temp_array);
15✔
40
        }
41

42
        return $temp_array;
16✔
43
    }
44

45
    /**
46
     * Returns a row of a multidimensional array by field value. Returns false, if no row found.
47
     *
48
     * @param string|int $key      The array key (field name)
49
     * @param mixed      $value
50
     * @param array      $haystack a multidimensional array
51
     *
52
     * @return array|false
53
     */
54
    public static function getArrayRowByFieldValue($key, $value, array $haystack)
55
    {
56
        foreach ($haystack as $row) {
14✔
57
            if (!\is_array($row)) {
14✔
58
                continue;
×
59
            }
60

61
            if (!isset($row[$key])) {
14✔
62
                continue;
2✔
63
            }
64

65
            if ($row[$key] == $value) {
12✔
66
                return $row;
11✔
67
            }
68
        }
69

70
        return false;
4✔
71
    }
72
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc