<?php

$a = array('key1'=>1, 'a', 7, 'b', 'key2'=>8, 5, 12, 3);

$minKey = $maxKey = key ($a);
$minIndex = $maxIndex = 0;
$count=0;
while (($el = each($a)) !== false) {
	list($key,$value)=$el;
	if ($a[$minKey] > $value) {
	  $minKey = $key;
	  $minIndex = $count;
	}
	if ($a[$maxKey] < $value) {
	  $maxKey = $key;
	  $maxIndex = $count;
	}
	++$count;
}

$count = max(0,abs ($minIndex-$maxIndex)-1);
$minIndex = min($minIndex, $maxIndex)+1;

$a = array_merge (array_slice($a, 0, $minIndex, true)
                  ,array_reverse (array_slice($a, $minIndex, $count, true), true)
                  ,array_slice($a, $minIndex+$count, null, true)
                 );

foreach ($a as $key=>$value)
  echo "$key=>$value\n";
  