Mario: Array Sortier- und Manipulierproblem

Beitrag lesen

Liebe Freunde, die Tipps waren wie immer brauchbar. Vielen vielen Dank. Und hier die fertige Lösung:

  
$sortstring  = "12,24,27,28,32,45";  
$sortliste   = explode(',', $sortstring);  
$wert1       = 45;    // Wert 1 soll  
$vor_nach    = 1;  // vor (1) oder nach (2)  
$wert2       = 27;    // Wer 2 platziert werden.  
  
$sortliste2  = arraysort($sortliste, $wert1, $wert2, $vor_nach);  
  
$sortstring2 = implode(',',$sortliste2);  
echo $sortstring2;  
exit();  
  
function arraysort($sortarr, $schieb, $platz, $action){  
    $pos_schieb=array_search($schieb, $sortarr);  
    $pos_platz=array_search($platz, $sortarr);  
  
    if($pos_schieb<$pos_platz and $action==1){  
      for($x=0;$x<$pos_schieb;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      for($x=$pos_schieb+1;$x<$pos_platz;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      $sortnew[]=$schieb;  
      for($x=$pos_platz;$x<count($sortarr);$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
    }  
  
    elseif($pos_schieb>$pos_platz and $action==1){  
      for($x=0;$x<$pos_platz;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      $sortnew[]=$schieb;  
      for($x=$pos_platz;$x<$pos_schieb;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      for($x=$pos_schieb+1;$x<count($sortarr);$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
    }  
  
    elseif($pos_schieb<$pos_platz and $action==2) {  
      for($x=0;$x<$pos_schieb;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      for($x=$pos_schieb+1;$x<$pos_platz+1;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      $sortnew[]=$schieb;  
      for($x=$pos_platz+1;$x<count($sortarr);$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
    }  
  
    elseif($pos_schieb>$pos_platz and $action==2){  
      for($x=0;$x<$pos_platz+1;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      $sortnew[]=$schieb;  
      for($x=$pos_platz+1;$x<$pos_schieb;$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
      for($x=$pos_schieb+1;$x<count($sortarr);$x++){  
        $sortnew[]=$sortarr[$x];  
      }  
    }  
    elseif($pos_schieb==$pos_platz){  
      $sortnew=$sortarr;  
    }  
    unset($sortarr);  
    return $sortnew;  
}