Arrays::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
15
// +----------------------------------------------------------------------
16
17
declare (strict_types=1);
18
19
namespace DtApp\ThinkLibrary\helper;
20
21
/**
22
 * 数组管理类
23
 * @mixin Arrays
24
 * @package DtApp\ThinkLibrary\helper
25
 */
26
class Arrays
27
{
28
    /**
29
     * 数组随机返回一个下标
30
     * @param $array
31
     * @return mixed
32
     */
33
    public function rand(array $array)
34
    {
35
        return array_rand($array);
36
    }
37
38
    /**
39
     * 数组随机返回一个值
40
     * @param $array
41
     * @return mixed
42
     */
43
    public function randValue(array $array)
44
    {
45
        return $array[array_rand($array)];
46
    }
47
48
    /**
49
     * 分隔数组
50
     * @param array $array 数组
51
     * @param int $num 数量
52
     * @return array
53
     */
54
    public function split(array $array, $num = 5): array
55
    {
56
        $arrRet = array();
57
        if (!isset($array) || empty($array)) {
58
            return $arrRet;
59
        }
60
        $iCount = count($array) / $num;
61
        if (!is_int($iCount)) {
0 ignored issues
show
introduced by
The condition is_int($iCount) is always true.
Loading history...
62
            $iCount = ceil($iCount);
63
        } else {
64
            ++$iCount;
65
        }
66
        for ($i = 0; $i < $iCount; ++$i) {
67
            $arrInfos = array_slice($array, $i * $num, $num);
68
            if (empty($arrInfos)) {
69
                continue;
70
            }
71
            $arrRet[] = $arrInfos;
72
            unset($arrInfos);
73
        }
74
        return $arrRet;
75
    }
76
77
    /**
78
     * 多维数组去重
79
     * @param array $array
80
     * @return array
81
     */
82
    public function unique(array $array): array
83
    {
84
        $out = array();
85
        foreach ($array as $key => $value) {
86
            if (!in_array($value, $out)) {
87
                $out[$key] = $value;
88
            }
89
        }
90
        $out = array_values($out);
91
        return $out;
92
    }
93
94
    /**
95
     * 二维数组根据某个键排序
96
     * @param array $arrays
97
     * @param string $sort_key
98
     * @param int $sort_order
99
     * @param int $sort_type
100
     * @return array
101
     */
102
    public function sort(array $arrays, string $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC): array
103
    {
104
        $key_arrays = array();
105
        if (is_array($arrays)) {
0 ignored issues
show
introduced by
The condition is_array($arrays) is always true.
Loading history...
106
            foreach ($arrays as $array) {
107
                if (is_array($array)) {
108
                    $key_arrays[] = $array[$sort_key];
109
                } else {
110
                    return [];
111
                }
112
            }
113
        } else {
114
            return [];
115
        }
116
        array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
117
        return $arrays;
118
    }
119
120
    /**
121
     * 数组删除空格
122
     * @param array $arr
123
     * @return array
124
     */
125
    public function trimArray(array $arr)
126
    {
127
        if (!is_array($arr)) {
0 ignored issues
show
introduced by
The condition is_array($arr) is always true.
Loading history...
128
            return $arr;
129
        }
130
        foreach ($arr as $key => $value) {
131
            if (is_array($value)) {
132
                $arr[$key] = $this->TrimArray($value);
133
            } else {
134
                $arr[$key] = $this->trimAll($value);
135
            }
136
        }
137
        return $arr;
138
    }
139
140
    /**
141
     * 字符串删除空格
142
     * @param $str
143
     * @return string|string[]
144
     */
145
    private function trimAll($str)
146
    {
147
        $oldchar = array(" ", " ", "\t", "\n", "\r");
148
        $newchar = array("", "", "", "", "");
149
        return str_replace($oldchar, $newchar, $str);
150
    }
151
152
    /**
153
     * 把json字符串或json对象转json数组
154
     * @param $output
155
     * @return array
156
     */
157
    public function toArray($output): array
158
    {
159
        if (is_array($output)) {
160
            return $output;
161
        }
162
        if (is_object($output)) {
163
            $output = json_encode($output, JSON_UNESCAPED_UNICODE);
164
        }
165
        return json_decode($output, true);
166
    }
167
168
    /**
169
     * @param $array
170
     * @return array
171
     */
172
    public function valChunk($array,$name): array
173
    {
174
        $result = array();
175
        $ar2 = [];
176
        foreach ($array as $key => $value) {
177
            foreach ($array as $k => $val) {
178
                if ($value["{$name}"] == $val["{$name}"]) {
179
                    $ar2[] = $val;
180
                }
181
            }
182
            $result[$value["{$name}"]] = $ar2;
183
            $ar2 = [];
184
        }
185
        return $result;
186
    }
187
}
188