fork download
  1. <?php
  2.  
  3. function myAsort(array &$arr = array())
  4. {
  5. $cnt = 0;
  6. foreach($arr as $key=>$value)
  7. {
  8. $keys[] = $key;
  9. $values[] = $value;
  10. ++$cnt;
  11. }
  12.  
  13. for($i = 1; $i < $cnt; ++$i)
  14. {
  15. for($j = $i; $j > 0 && $values[$j-1] > $values[$j]; --$j)
  16. {
  17. $tmpIndex = $j - 1;
  18.  
  19. $tmp = $values[$tmpIndex];
  20. $values[$tmpIndex] = $values[$j];
  21. $values[$j] = $tmp;
  22.  
  23. $tmp = $keys[$tmpIndex];
  24. $keys[$tmpIndex] = $keys[$j];
  25. $keys[$j] = $tmp;
  26.  
  27. }
  28. }
  29.  
  30. $arr = [];
  31.  
  32. for($i = 0; $i < $cnt; ++$i)
  33. $arr[$keys[$i]] = $values[$i];
  34. }
  35.  
  36.  
  37. $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
  38. myAsort($fruits);
  39. var_export($fruits);
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
array (
  'c' => 'apple',
  'b' => 'banana',
  'd' => 'lemon',
  'a' => 'orange',
)