Passed
Push — v6 ( acff9b...736453 )
by 光春
02:38
created

Arrays::trimAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
declare (strict_types=1);
21
22
namespace DtApp\ThinkLibrary\helper;
23
24
/**
25
 * 数组管理类
26
 * @mixin Arrays
27
 * @package DtApp\ThinkLibrary\helper
28
 */
29
class Arrays
30
{
31
    /**
32
     * 数组随机返回一个下标
33
     * @param $array
34
     * @return mixed
35
     */
36
    public function rand(array $array)
37
    {
38
        return array_rand($array);
39
    }
40
41
    /**
42
     * 数组随机返回一个值
43
     * @param $array
44
     * @return mixed
45
     */
46
    public function randValue(array $array)
47
    {
48
        return $array[array_rand($array)];
49
    }
50
51
    /**
52
     * 分隔数组
53
     * @param array $array 数组
54
     * @param int $num 数量
55
     * @return array
56
     */
57
    public function split(array $array, $num = 5): array
58
    {
59
        $arrRet = array();
60
        if (!isset($array) || empty($array)) {
61
            return $arrRet;
62
        }
63
        $iCount = count($array) / $num;
64
        if (!is_int($iCount)) {
0 ignored issues
show
introduced by
The condition is_int($iCount) is always true.
Loading history...
65
            $iCount = ceil($iCount);
66
        } else {
67
            $iCount += 1;
68
        }
69
        for ($i = 0; $i < $iCount; ++$i) {
70
            $arrInfos = array_slice($array, $i * $num, $num);
71
            if (empty($arrInfos)) {
72
                continue;
73
            }
74
            $arrRet[] = $arrInfos;
75
            unset($arrInfos);
76
        }
77
        return $arrRet;
78
    }
79
80
    /**
81
     * 多维数组去重
82
     * @param array $array
83
     * @return array
84
     */
85
    public function unique(array $array)
86
    {
87
        $out = array();
88
        foreach ($array as $key => $value) {
89
            if (!in_array($value, $out)) {
90
                $out[$key] = $value;
91
            }
92
        }
93
        $out = array_values($out);
94
        return $out;
95
    }
96
97
    /**
98
     * 二维数组根据某个键排序
99
     * @param array $arrays
100
     * @param string $sort_key
101
     * @param int $sort_order
102
     * @param int $sort_type
103
     * @return array
104
     */
105
    public function sort(array $arrays, string $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
106
    {
107
        $key_arrays = array();
108
        if (is_array($arrays)) {
0 ignored issues
show
introduced by
The condition is_array($arrays) is always true.
Loading history...
109
            foreach ($arrays as $array) {
110
                if (is_array($array)) {
111
                    $key_arrays[] = $array[$sort_key];
112
                } else {
113
                    return [];
114
                }
115
            }
116
        } else {
117
            return [];
118
        }
119
        array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
120
        return $arrays;
121
    }
122
123
    /**
124
     * 数组删除空格
125
     * @param array $arr
126
     * @return array
127
     */
128
    public function trimArray(array $arr)
129
    {
130
        if (!is_array($arr)) {
0 ignored issues
show
introduced by
The condition is_array($arr) is always true.
Loading history...
131
            return $arr;
132
        }
133
        foreach ($arr as $key => $value) {
134
            if (is_array($value)) {
135
                $arr[$key] = $this->TrimArray($value);
136
            } else {
137
                $arr[$key] = $this->trimAll(trim($value));
138
            }
139
        }
140
        return $arr;
141
    }
142
143
    /**
144
     * 字符串删除空格
145
     * @param $str
146
     * @return string|string[]
147
     */
148
    private function trimAll($str)
149
    {
150
        $oldchar = array(" ", " ", "\t", "\n", "\r");
151
        $newchar = array("", "", "", "", "");
152
        return str_replace($oldchar, $newchar, $str);
153
    }
154
}
155