Files
bm/public_html/public/include/array.inc.php
2025-09-24 13:26:28 +02:00

70 lines
1.6 KiB
PHP

<?php
# fügt den Wert $value an der Stelle $pos in das Array $array ein
//-------------------------------------------
function array_insert($array,$pos,$value){
//-------------------------------------------
$new = array();
for($i = 0; $i<$pos; $i++){
$new[$i] = $array[$i];
}
$new[$pos] = $value;
for($i = $pos; $i<sizeof($array); $i++){
$new[$i+1] = $array[$i];
}
return $new;
}
# löscht einen Wert aus dem Array $array an der Stelle $pos
//------------------------
function array_remove($array,$pos){
//------------------------
$new = array();
for($i = 0; $i<$pos; $i++){
$new[$i] = $array[$i];
}
for($i = $pos+1; $i<sizeof($array); $i++){
$new[$i-1] = $array[$i];
}
return $new;
}
# vertauscht Werte eines Arrays
//---------------------------------------
function array_swap($array,$pos1,$pos2){
//---------------------------------------
$cache = $array[$pos1];
$array[$pos1] = $array[$pos2];
$array[$pos2] = $cache;
return $array;
}
# liefert Index des Wertes $needle im Array $haystack zurück
//------------------------------------------
function array_index_of($haystack,$needle){
//------------------------------------------
foreach($haystack as $i=>$j){
if($j == $needle)
return $i;
}
return -1;
}
# gibt das Array (und alle verschachtelten Arrays) als <ul> zurück
//-----------------------------------------
function array_print($arr){
//-----------------------------------------
$out = "<ul>";
foreach($arr as $val){
if(is_array($val)){
$out .= "<li>".array_print($val)."</li>";
}
else{
$out .= "<li>$val</li>";
}
}
$out .= "</ul>";
return $out;
}
?>