-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patharray.go
223 lines (193 loc) · 5.19 KB
/
array.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package phpfuncs
import (
"fmt"
"math"
"reflect"
"sort"
"strings"
)
// ArraySlice type -- Required for ArrayChunk
type ArraySlice []interface{}
// Array - Create an array
//
// Original : https://www.php.net/manual/en/function.array.php
func Array(v ...interface{}) []interface{} {
return v
}
// ArrayKeys - Return all the keys or a subset of the keys of an array
//
// Original : https://www.php.net/manual/en/function.array-keys.php
func ArrayKeys(v map[string]interface{}) []string {
var aRdata []string
for s := range v {
aRdata = append(aRdata, s)
}
return aRdata
}
// ArrayPush - Push one or more elements onto the end of array
//
// Original : https://www.php.net/manual/en/function.array-push.php
func ArrayPush(v *[]interface{}, data ...interface{}) {
*v = append(*v, data...)
}
// ArrayReverse - Return an array with elements in reverse order
//
// Original : https://www.php.net/manual/en/function.array-reverse.php
func ArrayReverse(v []interface{}) []interface{} {
for ind, fnd := 0, len(v); ind < fnd; ind, fnd = ind+1, fnd-1 {
v[ind], v[fnd] = v[fnd], v[ind]
}
return v
}
// ArrayCountValues - Counts all the values of an array
//
// Original : https://www.php.net/manual/en/function.array-count-values.php
func ArrayCountValues(v []interface{}) map[interface{}]uint {
cnt := make(map[interface{}]uint)
for _, s := range v {
if c, ok := cnt[s]; ok {
cnt[s] = c + 1
} else {
cnt[s] = 1
}
}
return cnt
}
// ArrayMerge - Merge one or more arrays
//
// Original : https://www.php.net/manual/en/function.array-merge.php
func ArrayMerge(v ...[]interface{}) []interface{} {
fArray := make([]interface{}, 0)
for _, m := range v {
fArray = append(fArray, m...)
}
return fArray
}
// ArrayValues - Return all the values of an array
//
// Original : https://www.php.net/manual/en/function.array-values.php
func ArrayValues(v ...[]interface{}) (value []interface{}) {
var values []interface{}
for _, val := range v {
cIn := InArray(val, v)
if !cIn {
values = append(values, val)
}
}
return values
}
// ArrayChunk - Split an array into chunks
//
// Original : https://www.php.net/manual/en/function.array-chunk.php
func ArrayChunk(v ArraySlice, size int) ArraySlice {
length := len(v)
count := int(math.Ceil(float64(length) / float64(size)))
ret := make(ArraySlice, count)
for i := 0; i < count-1; i++ {
ret[i] = v[i*size : (i+1)*size]
fmt.Println(ret)
}
ret[count-1] = v[size*(count-1):]
return ret
}
// ArrayFlip - Exchanges all keys with their associated values in an array
//
// Original : https://www.php.net/manual/en/function.array-flip.php
//
// ArrayFlip returns an array in flip order, i.e. keys from array become values and values from array become keys.
func ArrayFlip(v map[interface{}]interface{}) map[interface{}]interface{} {
z := make(map[interface{}]interface{})
for ind, fnd := range v {
z[fnd] = ind
}
return z
}
// Count - Count all elements in an array, or something in an object
//
// Original : https://www.php.net/manual/en/function.count.php
func Count(v []interface{}) int {
return len(v)
}
// Implode - Join array elements with a string.
//
// Original : https://www.php.net/manual/en/function.implode.php
//
// Join array elements with a glue string.
func Implode(sep string, v []string) string {
return strings.Replace(strings.Trim(fmt.Sprint(v), "[]"), " ", sep, -1)
}
// IsArray - Finds whether a variable is an array.
//
// Original : https://www.php.net/manual/en/function.is-array.php
//
// Finds whether the given variable is an array.
func IsArray(v interface{}) bool {
t := reflect.TypeOf(v)
switch t.Kind() {
case reflect.Slice:
return true
default:
}
return false
}
// InArray - Similar function of in_array in PHP
//
// Original : https://www.php.net/manual/en/function.in-array.php
//
// needle : string, int
// haystack : should be an array
// strict : set true for type check
// return boolean true / false
func InArray(needle interface{}, haystack interface{}) bool {
switch reflect.TypeOf(haystack).Kind() {
default:
s := reflect.ValueOf(haystack)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
return true
}
}
}
return false
}
// Sizeof - Alias of Count
//
// Original : https://www.php.net/manual/en/function.sizeof.php
func Sizeof(v []interface{}) int {
return Count(v)
}
// Sort - Sort an array
//
// Original : https://www.php.net/manual/en/function.sort.php
//
// This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.
//
// Flags are same as php sort.
// SORT_REGULAR - compare items normally;
// SORT_NUMERIC - compare items numerically
// SORT_STRING - compare items as strings
// SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
func Sort(v interface{}, flag string) bool {
art := reflect.TypeOf(v)
var va bool
if art.Kind() == reflect.Slice {
switch flag {
case "SORT_REGULAR":
array := v.([]string)
sort.Strings(array)
case "SORT_STRING":
array := v.([]string)
sort.Strings(array)
case "SORT_NUMERIC":
array := v.([]int)
sort.Ints(array)
default:
array := v.([]string)
sort.Strings(array)
}
va = true
} else {
va = false
}
return va
}