开发者

PHP Array sort by number of repeats

开发者 https://www.devze.com 2023-02-06 18:39 出处:网络
I have an array with non-unique values, I need the array to be unique and ordered by the number of times each value returns.

I have an array with non-unique values, I need the array to be unique and ordered by the number of times each value returns.

I have got to place where I have an array with every unique value and the number of times that this value repeated - problem is, I need the array ordered with the values when the most repeated is first and the lowest repeated at last. (used array_count_values)

The array has a lot of results to ha开发者_如何学Cndle..

un-ordered array:

array(14) {
  [0]=>
  int(1)
  [1]=>
  int(3)
  [2]=>
  int(4)
  [3]=>
  int(2)
  [4]=>
  int(3)
  [5]=>
  int(4)
  [6]=>
  int(5)
  [7]=>
  int(1)
  [8]=>
  int(2)
  [9]=>
  int(3)
  [10]=>
  int(4)
  [11]=>
  int(1)
  [12]=>
  int(2)
  [13]=>
  int(3)
}

Ordered array (value refers to repeats):

array(5) {
  [1]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(3)
  [2]=>
  int(3)
  [5]=>
  int(1)
}


I am not sure of your problem, wouldn't just asort() fix the array_count_values() array?

0

精彩评论

暂无评论...
验证码 换一张
取 消