Werbung

Werbung

Werbung

Archive

Folge uns auf Twitter

Werbung

Jede Menge neue Twitter Follower bekommen. Die Software hat einen kostenlosen Trial Mode, der bis zu 250 Follower bringt.

Willkommen

Willkommen im PHP Archiv Blog! Hier gibts Infos zum Thema PHP Codeschnipsel und Tipps. Bei unserem Projekt phparchiv.de, finden Sie über 5700 Scripte.

Array

Array splitten mit Random Split Funktion

Diese Funktion splittet ein Array in Teile mit zufälliger Länge

 

function RandomSplit($min, $max, $str){
$a = array();

while ($str != ''){
$p = rand($min, $max);
$p = ($p > strlen($str)) ? strlen($str) : $p;

$buffer = substr($str, 0, $p);
$str = substr($str, $p, strlen($str)-$p);

$a[] = $buffer;
}
return $a;
}[/php]

Quelle

Foreach mit einem leeren Array

Normalerweise gibt PHP bei einem foreach mit einem leeren Array einen Fehler aus. Mit dieser Funktion kann man das umgehen.

 

foreach ((array) $non_array as $key => $val) {
print "Key $key, Value $val\n";
}
?>[/php]

Dropdown Menu aus einem Array erstellen

Diese Funktion zeigt wie man ein Dropdown Menu aus einem Array erstellt


//Array contents array 1 :: value
$myArray1 = array('Cat','Mat','Fat','Hat');
//Array contents array 2 :: key => value
$myArray2 = array('c'=>'Cat','m'=>'Mat','f'=>'Fat','h'=>'Hat');

//Values from array 1
echo'<select name="Words">';
//for each value of the array assign a variable name word
foreach($myArray1 as $word){
echo'<option value="'.$word.'">'.$word.'</option>';
}
echo'</select>';

//Values from array 2
echo'<select name="Words">';
//for each key of the array assign a variable name let
//for each value of the array assign a variable name word
foreach($myArray2 as $let=>$word){
echo'<option value="'.$let.'">'.$word.'</option>';
}
echo'</select>';
?>[/php]
Quelle: phpsnips.com

Ein Dropdown Menu aus einem Array erstellen

Das kleine Script erzeugt ein Drop Down Menu aus einem Array


//Array contents array 1 :: value
$myArray1 = array('Cat','Mat','Fat','Hat');
//Array contents array 2 :: key => value
$myArray2 = array('c'=>'Cat','m'=>'Mat','f'=>'Fat','h'=>'Hat');

//Values from array 1
echo'<select name="Words">';
//for each value of the array assign a variable name word
foreach($myArray1 as $word){
echo'<option value="'.$word.'">'.$word.'</option>';
}
echo'</select>';

//Values from array 2
echo'<select name="Words">';
//for each key of the array assign a variable name let
//for each value of the array assign a variable name word
foreach($myArray2 as $let=>$word){
echo'<option value="'.$let.'">'.$word.'</option>';
}
echo'</select>';
?>[/php]

Die Idee stammt con phpsnips.com