fork(3) download
  1. <?php
  2.  
  3. $a = array('key1'=>1, 'a', 7, 'b', 'key2'=>8, 5, 12, 3);
  4.  
  5. $minKey = $maxKey = key ($a);
  6. $minIndex = $maxIndex = 0;
  7. $count=0;
  8. while (($el = each($a)) !== false) {
  9. list($key,$value)=$el;
  10. if ($a[$minKey] > $value) {
  11. $minKey = $key;
  12. $minIndex = $count;
  13. }
  14. if ($a[$maxKey] < $value) {
  15. $maxKey = $key;
  16. $maxIndex = $count;
  17. }
  18. ++$count;
  19. }
  20.  
  21. $count = max(0,abs ($minIndex-$maxIndex)-1);
  22. $minIndex = min($minIndex, $maxIndex)+1;
  23.  
  24. $a = array_merge (array_slice($a, 0, $minIndex, true)
  25. ,array_reverse (array_slice($a, $minIndex, $count, true), true)
  26. ,array_slice($a, $minIndex+$count, null, true)
  27. );
  28.  
  29. foreach ($a as $key=>$value)
  30. echo "$key=>$value\n";
  31.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
key1=>1
0=>a
1=>5
key2=>8
2=>b
3=>7
4=>12
5=>3