0

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 handle..

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)
}
1
  • Is that bottom array supposed to go 3, 4, 3, 3, 1? Commented Jan 23, 2011 at 0:39

1 Answer 1

1

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

2
  • Correct. it's just array_count_values and asort: $freqs = array_count_values($array); asort($freqs);
    – doc_id
    Commented Jan 23, 2011 at 1:06
  • Becuase I need to sort the array by number of repeats, but in the same time I need to work with the ids that saved in the value.
    – user586037
    Commented Jan 23, 2011 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.