<?php

$a = array('key1'=>1, 12, 7, 3.14, 'key2'=>8, 5, 'a', 3);
var_dump($a);
$minKey = $maxKey = key ($a);
$minIndex = $maxIndex = 0;
for ($count=0; ($el = each($a)) !== false; ++$count) {
	list($key,$value)=$el;
	if ($a[$minKey] > $value) {
	  $minKey = $key;
	  $minIndex = $count;
	}
	if ($a[$maxKey] < $value) {
	  $maxKey = $key;
	  $maxIndex = $count;
	}
}

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

$b = array();
foreach ($a as $key=>$value)
	$b[$key] = $value;
	
$a = array();
for ($i=0; $i < $minIndex; ++$i) {
	list($key,$value)=each($b);
	$a[$key]=$value;
}
for ($c=array(); $i < $minIndex+$count; ++$i) {
	list($key,$value)=each($b);
	$c[$key]=$value;
}
for (end($c); !is_null($key=key($c)); prev($c)) {
    $val = current($c);
    $a[$key]=$val;
}
while (($el = each($b)) !== false) {
	list($key,$value)=$el;
	$a[$key]=$value;
}
var_dump($a);  