数组去重并总计重复成分出现的次数

2、方法二(set方法去重)

  • when 方法

1、方法一

  • where 方法

 

# 严格过滤 - 参看whereIn方法 /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param mixed $values * @return static */ public function whereInStrict($key, $values) { return $this->whereIn($key, $values, true); }

 

  • mode 方法

 

  • max 方法

3、方法三(使用对象记录重复的元素,以及出现的次数)

  • tap 方法
var arr = [1, 2, 3, 1, 2, 4]; function arrayCnt(arr) { var newArr = []; //使用set进行数组去重 newArr = [...new Set(arr)]; var newarr2 = new Array(newArr.length); for(var t = 0; t < newarr2.length; t++) { newarr2[t] = 0; } for(var p = 0; p < newArr.length; p++) { for(var j = 0; j < arr.length; j++) { if(newArr[p] == arr[j]) { newarr2[p]++; } } } for(var m = 0; m < newArr.length; m++) { console.log(newArr[m] + "重复的次数为:" + newarr2[m]); } } arrayCnt(arr);

var arr = [1, 2, 3, 1, 2, 4]; function arrayCnt(arr) { var newArr = []; for(var i = 0; i < arr.length; i++) { if(newArr.indexOf(arr[i]) == -1) { newArr.push(arr[i]) } } var newarr2 = new Array(newArr.length); for(var t = 0; t < newarr2.length; t++) { newarr2[t] = 0; } for(var p = 0; p < newArr.length; p++) { for(var j = 0; j < arr.length; j++) { if(newArr[p] == arr[j]) { newarr2[p]++; } } } for(var m = 0; m < newArr.length; m++) { console.log(newArr[m] + "重复的次数为:" + newarr2[m]); } } arrayCnt(arr);
# 返回出现次数最多的数$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));dump($collection->mode('php')); //> [13]$collection = collect(array('hello'=>34,'php'=>2,'we'=>34,'ed'=>34,'wd'=>3,2,2));dump($collection->mode()); //> [34,2]$collection = collect(array(12,3,234,21,32,123,1,1,2,2));dump($collection->mode()); //> [1,2]

 

//方法:使用obj记录重复的元素,以及出现的次数 function getCount(arr) { var obj = {}, k, arr1 = []; for(var i = 0, len = arr.length; i < len; i++) { k = arr[i]; if(obj[k]) obj[k]++; else obj[k] = 1; } console.log(obj) //保存结果{el-'元素',count-出现次数} for(var o in obj) { arr1.push({ el: o, count: obj[o] }); } console.log(arr1); } getCount([4, 2, 2, 3, 1])
# 源码 - when 方法使用很简单这里不详细举例了 /** * Apply the callback if the value is truthy. * * @param bool $value # 判断条件 * @param callable $callback * @param callable $default * @return mixed */ public function when($value, callable $callback, callable $default = null) { if ($value) { return $callback($this); } elseif ($default) { return $default($this); } return $this; }

 /** * Pass the collection to the given callback and then return it. * * @param callable $callback * @return $this */ public function tap(callable $callback) { $callback(new static($this->items)); return $this; } $collection = collect(array('hello'=>"miss",'php'=>2,'or',3)); $collection->tap(function($coll){ dump($coll); //> collect(array('hello'=>"miss",'php'=>2,'or',3)) });

# 该方法和whereInStrict用法相反 /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param mixed $values * @return static */ public function whereNotInStrict($key, $values) { return $this->whereNotIn($key, $values, true); }
  • 创建集合
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->sum();dump($collection); //> 5$collection = collect(array(1,3,5,7,9))->sum();dump($collection); //> 25$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->sum('php');dump($collection); //> 44
  • whereIn 方法

# max 推荐纯数值集合$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->max(); //> 不推荐dump($collection); //> 3$collection = collect(array(1,3,5,7,9))->max();dump($collection); //> 9$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->max('php');dump($collection); //> 32

  • whereNotIn 方法
  • all 方法
# 检查集合中每一个元素是否符合条件# 源码参考 /** * Determine if all items in the collection pass the given test. * * @param string|callable $key * @param mixed $operator * @param mixed $value * @return bool */ public function every($key, $operator = null, $value = null) { if (func_num_args() == 1) { $callback = $this->valueRetriever($key); foreach ($this->items as $k => $v) { if (! $callback($v, $k)) { return false; } } return true; } if (func_num_args() == 2) { $value = $operator; $operator = '='; } return $this->every($this->operatorForWhere($key, $operator, $value)); }# 使用示例$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));$result = $collection->every(function($v,$k){ # $v 值 $k 键 # 条件判断 # return false; 比较失败终止比较返回false # return true; 本次比较通过,进行下次比较 return true;});dump($result); //> truedump($collection->every('php','>',12)); //> false# php键 是否存在集合里(集合里的每一个数组中)dump($collection->every('php')); //> true dump(collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)))->every('php')); //> false# 支持比较符 null(=) = == != <> > < >= <= === !==# 更多用法 - 参看源码

  • toJson 方法

# 过滤集合中元素参看 array_filter() 方法$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));# 遍历集合数组元素(返回插入集合数值)$coll = $collection->reject(function ($value,$key){ # $value 值 $key 键 # 返回值 bool:true 过滤该值 if( $key === 'php' ){ return true; } return false;});dump($coll); //> collect(array('hello'=>"miss,'or','golang'))

# min 推荐纯数值集合$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->min(); //> 不推荐dump($collection); //> miss$collection = collect(array(1,3,5,7,9))->min();dump($collection); //> 1$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->min('php');dump($collection); //> 12

  • unless 方法
 /** * Get the collection of items as JSON. * * @param int $options * @return string */ public function toJson($options = 0) { return json_encode($this->jsonSerialize(), $options); } /** * Convert the object into something JSON serializable. * * @return array */ public function jsonSerialize() { return array_map(function ($value) { if ($value instanceof JsonSerializable) { return $value->jsonSerialize(); } elseif ($value instanceof Jsonable) { return json_decode($value->toJson(), true); } elseif ($value instanceof Arrayable) { return $value->toArray(); } else { return $value; } }, $this->items); }

# 源码 - 给定集合键(过滤集合)是否在另一个数组或集合里存在 /** * Filter items by the given key value pair. * * @param string $key # 给定集合里的键 * @param mixed $values # 另一个集合或数组 * @param bool $strict # 是否开启严格比较 * @return static */ public function whereIn($key, $values, $strict = false) { $values = $this->getArrayableItems($values); return $this->filter(function ($item) use ($key, $values, $strict) { return in_array(data_get($item, $key), $values, $strict); }); }
  • sum 方法
# 参考where方法 严格等于 筛选 /** * Filter items by the given key value pair using strict comparison. * * @param string $key * @param mixed $value * @return static */ public function whereStrict($key, $value) { return $this->where($key, '===', $value); }
$collection = collect(array('hello'=>"miss",'php'=>2,'or',3))->avg(); //> 不推荐dump($collection); //> 1.25 = (2+3)/4$collection = collect(array(1,3,5,7,9))->avg();dump($collection); //> 5 = (1+3+5+7+9)/5$collection = collect(array(array('php'=>12,13,43),array('es'=>12,13,'php'=>32)))->avg('php');dump($collection); //> 22 = (12+32)/2
# 获取给定集合的中间值(排序后的中间值)# 集合是奇数 获取中间数值# 集合是偶数 获取中间两个数的平均值$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3,43));dump($collection->median()); //> 33 = (34+32)/2$collection = collect(array('hello'=>34,'php'=>2,'we'=>32,'ed'=>54,'wd'=>3));dump($collection->median()); //> 32$collection = collect(array(array(12,11,'php'=>43),array(12,1,'php'=>23),array('php'=>12,3)));dump($collection->median('php')); //> 23

# 该方法正好和whereIn用法相反 /** * Filter items by the given key value pair. * * @param string $key * @param mixed $values * @param bool $strict * @return static */ public function whereNotIn($key, $values, $strict = false) { $values = $this->getArrayableItems($values); return $this->reject(function ($item) use ($key, $values, $strict) { return in_array(data_get($item, $key), $values, $strict); }); }
# 过滤掉其他数据 刷选 /** * Filter items by the given key value pair. * * @param string $key * @param mixed $operator * @param mixed $value * @return static */ public function where($key, $operator, $value = null) { if (func_num_args() == 2) { $value = $operator; $operator = '='; } return $this->filter($this->operatorForWhere($key, $operator, $value)); }# 使用示例$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array(3)));dump($collection->where('php','=',13)); //> collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13)))# 支持比较符 null(=) = == != <> > < >= <= === !==
  • toArray 方法

  • each 方法
# 参看 array_map() 函数$collection = collect(array('hello'=>"miss",'php'=>"mr",'or','golang'));# 遍历集合数组元素(返回插入集合数值)$coll = $collection->map(function ($value,$key){ # $value 值 $key 键 return "a12";});dump($coll); //> collect(array('hello'=>"a12",'php'=>"a12","a12","a12"))
  • map 方法

  • whereNotInStrict 方法
 /** * Execute a callback over each item. * * @param callable $callback * @return $this */ public function each(callable $callback) { foreach ($this->items as $key => $item) { if ($callback($item, $key) === false) { break; } } return $this; }$collection = collect(array(array(12,11,'php'=>13),array(12,1,'php'=>13),array('php'=>12,3)));$collection->each(function ($value,$key){ # $value 值 $key 键 dump($value); dump($key);});
  • whereInStrict 方法
 /** * @return array * 得到当前Collection的值 $this->items */ public function all();
  • median 方法
  • every 方法
 /** * Zip the collection together with one or more arrays. * * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); * => [[1, 4], [2, 5], [3, 6]] * * @param mixed ...$items * @return static */ public function zip($items) { $arrayableItems = array_map(function ($items) { return $this->getArrayableItems($items); }, func_get_args()); $params = array_merge([function () { return new static(func_get_args()); }, $this->items], $arrayableItems); return new static(call_user_func_array('array_map', $params)); }
 /** * @param mixed $value * @return \Illuminate\Support\Collection * 辅助函数 collect 创建 Collection 对象 */ Collection function collect($value = null); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /** * @param mixed $value * @return \Illuminate\Support\Collection * 采用 new 关键字创建Collection集合对象 */ new \Illuminate\Support\Collection($value);
  • zip 方法
  • whereStrict 方法

  • avg 方法
# 源码 - 参考when方法 /** * Apply the callback if the value is falsy. * * @param bool $value # 判断条件 * @param callable $callback * @param callable $default * @return mixed */ public function unless($value, callable $callback, callable $default = null) { return $this->when(! $value, $callback, $default); }

  • reject 方法

 /** * Get the collection of items as a plain array. * * @return array */ public function toArray() { return array_map(function ($value) { return $value instanceof Arrayable ? $value->toArray() : $value; }, $this->items); }
  • min 方法

相关文章

Comment ()
评论是一种美德,说点什么吧,否则我会恨你的。。。