| Element |
11-16-2005 10:29 PM |
Random Array Key
Well for some reason I was asked this question. To me it doesn't seem hard, but obviously people have their own ways of learning. To create a function that picks a random "Key" from an array do something similar to the fallowing...
PHP Code:
<?php
$array = array("Who?", "What?", "Where?", "When?");
function RandomArrayKey($array) {
$max_keys = array_count($array);
$random_key = mt_rand(0, $max_keys);
return $array[$random_key];
}
?>
This is usefull if you want to randomize an array an display it. These arrays could contain image tags, html and text or numbers. Use how you see fit. And if someone has a method for dynamic arrays with arrays within arrays they are welcome to share, of course.
example code:
PHP Code:
<?php
include_once("functions.php") // Our random array function is in here
$favorite_foods = array("Pizza", "Cheese Burgers", "Philly Cheese Steak", "Cake");
echo RandomArrayKey($favorite_foods);
?>
This may be useless to alot of people, and you may have your other methods, but please, instead of correcting something you didn't submit, just post your method below. :) :thumbsup:
|