|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package utils
-
- // ContainsString is 字符串是否包含在字符串切片里
- func ContainsString(array []string, val string) (index int) {
- index = -1
- for i := 0; i < len(array); i++ {
- if array[i] == val {
- index = i
- return
- }
- }
- return
- }
-
- //ArrayDiff 模拟PHP array_diff函数
- func ArrayDiff(array1 []interface{}, othersParams ...[]interface{}) ([]interface{}, error) {
- if len(array1) == 0 {
- return []interface{}{}, nil
- }
- if len(array1) > 0 && len(othersParams) == 0 {
- return array1, nil
- }
- var tmp = make(map[interface{}]int, len(array1))
- for _, v := range array1 {
- tmp[v] = 1
- }
- for _, param := range othersParams {
- for _, arg := range param {
- if tmp[arg] != 0 {
- tmp[arg]++
- }
- }
- }
- var res = make([]interface{}, 0, len(tmp))
- for k, v := range tmp {
- if v == 1 {
- res = append(res, k)
- }
- }
- return res, nil
- }
-
- //ArrayIntersect 模拟PHP array_intersect函数
- func ArrayIntersect(array1 []interface{}, othersParams ...[]interface{}) ([]interface{}, error) {
- if len(array1) == 0 {
- return []interface{}{}, nil
- }
- if len(array1) > 0 && len(othersParams) == 0 {
- return array1, nil
- }
- var tmp = make(map[interface{}]int, len(array1))
- for _, v := range array1 {
- tmp[v] = 1
- }
- for _, param := range othersParams {
- for _, arg := range param {
- if tmp[arg] != 0 {
- tmp[arg]++
- }
- }
- }
- var res = make([]interface{}, 0, len(tmp))
- for k, v := range tmp {
- if v > 1 {
- res = append(res, k)
- }
- }
- return res, nil
-
- }
|