package util
// InArray 翻译PHP方法 in_array
func InArray[T comparable](needle T, haystack []T) bool {
if len(haystack) <= 8 {
for _, value := range haystack {
if needle == value {
return true
}
}
return false
}
cache := make(map[T]bool, len(haystack))
for _, value := range haystack {
if _, ok := cache[value]; !ok {
cache[value] = true
}
}
return cache[needle]
}
// ArrayDiff 翻译PHP方法 array_diff
func ArrayDiff[V comparable](array1 []V, arrayOthers ...[]V) []V {
c := make(map[V]bool)
for i := 0; i < len(array1); i++ {
if _, hasKey := c[array1[i]]; hasKey {
c[array1[i]] = true
} else {
c[array1[i]] = false
}
}
for i := 0; i < len(arrayOthers); i++ {
for j := 0; j < len(arrayOthers[i]); j++ {
if _, hasKey := c[arrayOthers[i][j]]; hasKey {
c[arrayOthers[i][j]] = true
} else {
c[arrayOthers[i][j]] = false
}
}
}
r := make([]V, 0)
for k, v := range c {
if !v {
r = append(r, k)
}
}
return r
}
// ArrayIntersect 翻译PHP方法 array_intersect
func ArrayIntersect[V comparable](array1 []V, arrayOthers ...[]V) []V {
c := make(map[V]bool)
for i := 0; i < len(array1); i++ {
if _, hasKey := c[array1[i]]; hasKey {
c[array1[i]] = true
} else {
c[array1[i]] = false
}
}
for i := 0; i < len(arrayOthers); i++ {
for j := 0; j < len(arrayOthers[i]); j++ {
if _, hasKey := c[arrayOthers[i][j]]; hasKey {
c[arrayOthers[i][j]] = true
} else {
c[arrayOthers[i][j]] = false
}
}
}
r := make([]V, 0)
for k, v := range c {
if v {
r = append(r, k)
}
}
return r
}
// ArrayColumn 翻译PHP方法 array_column
func ArrayColumn[K comparable, T any](input []map[K]T, columnKey K) []T {
output := make([]T, 0)
for _, item := range input {
if value, ok := item[columnKey]; ok {
output = append(output, value)
}
}
return output
}
func ArrayUnique[T comparable](input []T) []T {
unique := make([]T, 0, len(input))
existing := make(map[T]bool)
for _, v := range input {
if !existing[v] {
unique = append(unique, v)
existing[v] = true
}
}
return unique
}
// ArrayKey returns the keys of the map m.
// The keys will be in an indeterminate order.
func ArrayKey[M ~map[K]V, K comparable, V any](m M) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
// ArrayValue returns the values of the map m.
// The values will be in an indeterminate order.
// sort
func ArrayValue[M ~map[K]V, K comparable, V any](m M, sort ...K) []V {
r := make([]V, 0, len(m))
if len(sort) != 0 {
for _, id := range sort {
if v, ok := m[id]; ok {
r = append(r, v)
}
}
} else {
for _, v := range m {
r = append(r, v)
}
}
return r
}
func ArrayChunk[T any](input []T, size int) [][]T {
chunks := make([][]T, 0, (len(input)+size-1)/size)
for len(input) >= size {
chunks = append(chunks, input[:size:size])
input = input[size:]
}
if len(input) > 0 {
chunks = append(chunks, input[:len(input):len(input)])
}
return chunks
}
func ArraySlice[T any](s []T, offset, length uint) []T {
if offset > uint(len(s)) {
return s[:0]
}
end := offset + length
if end < uint(len(s)) {
return s[offset:end]
}
return s[offset:]
}
golang翻译php常用数组函数in_array,array_unique等
于 2023-10-26 19:24:35 首次发布