SHARE
TWEET

Sorting

MartinGeorgiev Jul 10th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $inputArr = [2, 2, 2, 2, 3, 3, 3, 1, 1, 1, 6, 6, 5, 5, 5];
  4. $arr = [];
  5. foreach ($inputArr as $item) {
  6.     if (key_exists($item, $arr)) {
  7.         $arr[$item]++;
  8.     } else {
  9.         $arr[$item] = 1;
  10.     }
  11. }
  12. ksort($arr);
  13. asort($arr);
  14. foreach ($arr as $number=>$repeats){
  15.     echo str_repeat($number." ",$repeats); // 6 6 1 1 1 3 3 3 5 5 5 2 2 2 2
  16. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top