Array
(
[a] => Apfel
[b] => Birne
[c] => Citrone
[d] => Dattel
[e] => Erbse
[f] => Feige
[g] => Gurke
[h] => Hagebutte
)
Array
(
[b] => Birne
[g] => Gurke
[e] => Erbse
[d] => Dattel
[f] => Feige
[c] => Citrone
[a] => Apfel
[h] => Hagebutte
)
Wir sind jetzt bei: Erbse
Der gegebene Schlüssel ist nicht vorhanden!
Jetzt sidn wir hier: Erbse
<?php
$array = array('a' => 'Apfel', 'b' => 'Birne', 'c' => 'Citrone', 'd' => 'Dattel',
'e' => 'Erbse', 'f' => 'Feige', 'g' => 'Gurke', 'h' => 'Hagebutte');
function out($arr) {
echo '<pre>';
print_r($arr);
echo '</pre><hr>';
}
/**
array array_shuffle(array array[, bool assoc]) -- Shuffles an array and keeps its indices
The difference to shuffle() is just the second parameter. By default, it's set to TRUE.
Then the array gets shuffled, but the association is still kept. When the parameter is set
to FALSE, this function just uses suffle() and returns the array.
by David Tibbe, 2004/09/09
**/
function array_shuffle($array, $assoc = TRUE) {
if ($assoc) {
$keys = array_keys($array);
$values = array_values($array);
$mix = range(0, sizeof($keys) - 1);
shuffle($mix);
unset($array);
foreach ($mix as $int) {
$array[$keys[$int]] = $values[$int];
}
} else {
shuffle($array);
}
return $array;
}
/**
array_goto(&array array, mixed key) -- Sets the internal array pointer on the
element given by the key
When key is not a key of array, this function returns FALSE (the pointer is not moved in
that case), otherwise TRUE and moves the internal array ponter to the element with the
key key.
by David Tibbe, 2004/09/10
**/
function array_goto(&$array, $key) {
if (!in_array($key, array_keys($array))) {
return FALSE;
} else {
reset($array);
$da = FALSE;
while (!$da) {
$pair = each($array);
if ($pair['key'] == $key) $da = TRUE;
}
prev($array);
return TRUE;
}
}
/**
!!!INCOMPLETE!!!
array_goby(&array array, int width[, bool forward[, bool loop]]) -- Moves the internal array
pointer width elements in
the given direction
By default, the direction ist forward (so this parameter is TRUE). When loop is TRUE (default
is FALSE) it begings at the first/last element again, otherwise it will stay after the last/before
the first one.
Raises a E_USER_WARNING when width is negative.
by David Tibbe, 2004/09/10
**/
function array_goby(&$array, $width, $forward = TRUE, $loop = FALSE) {
if (($width < 0) || (!is_int($width))) {
trigger_error('Negative/non-numeric value for $width: "'.$width.'"', E_USER_WARNING);
} else {
$gone = 0;
while ($gone != $width) {
if ($forward) {
next($array);
} else {
prev($array);
}
if (!each($array)) {
if ($loop) {
if ($forward) {
reset($array);
} else {
while (each($array)) { }
}
} else {
$gone = $width;
}
}
prev($array);
$gone++;
}
}
}
out($array);
$array_shuffled = array_shuffle($array, true);
out($array_shuffled);
if (array_goto($array, 'e'))
out('Wir sind jetzt bei: '.current($array));
if (!array_goto($array, 'o'))
out('Der gegebene Schlüssel ist nicht vorhanden!');
/* this does not work yet */
//array_goby($array, 5);
out('Jetzt sidn wir hier: '.current($array));
highlight_file(__FILE__);
?>