...

View Full Version : Random Array Key



Element
11-16-2005, 11:29 PM
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

$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

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:

marek_mar
11-17-2005, 12:11 AM
... either that or array_rand() (http://www.php.net/array_rand).

Element
11-17-2005, 08:25 AM
... either that or array_rand() (http://www.php.net/array_rand).

Oh, hey, thats usefull. See this is why i hate buying off-brand crap. That isn't under the array functions. Heck, even mysql_fetch_array() wasn't. :thumbsup:

marek_mar
11-17-2005, 04:16 PM
That isn't under the array functions.
It is there.
mysql_fetch_array() shouldn't be under arrays as it's a function which returnsdata from a resource.

Element
11-23-2005, 01:14 AM
It is there.
mysql_fetch_array() shouldn't be under arrays as it's a function which returnsdata from a resource.

Oh I know that, I ment it isn't under MySQL either. It just doesn't have it. It has the myswl_fetch_assoc() or w/e.

marek_mar
11-23-2005, 02:48 PM
mysql_fetch_array() is in PHP since PHP3. mysql_fetch_assoc() is from in PHP4.0.3.
mysql_fetch_assoc($result) is is the same as mysql_fetch_array($result, MYSQL_ASSOC)
Both are in the manual under the mysql extension functions.

Element
11-24-2005, 09:05 AM
mysql_fetch_array() is in PHP since PHP3. mysql_fetch_assoc() is from in PHP4.0.3.
mysql_fetch_assoc($result) is is the same as mysql_fetch_array($result, MYSQL_ASSOC)
Both are in the manual under the mysql extension functions.

Pshaw. Bre, your not listening. The book I have is a localy published paper-back off-brand meaning not something like O'Reily's stuff, private. It didn't have mysql_fetch_array() anywhere, so i was using mysql_fetch_assoc() until somone told me that it was much more "standard" to use mysql_fetch_array(), and he linked me to the function discovered it for the first time. This book I think was written in the eyes of one PHP coder with his own style, for lack of better words. I mean like how he does his code, and what functions he uses, etc.

marek_mar
11-24-2005, 04:01 PM
Well now I know what the "See this is why i hate buying off-brand crap" part ment...
Buying a PHP book that is just for function reference is IMO worthless as PHP has an excelent manual for them on the web which is kept up to date.

Element
11-24-2005, 09:11 PM
Well now I know what the "See this is why i hate buying off-brand crap" part ment...
Buying a PHP book that is just for function reference is IMO worthless as PHP has an excelent manual for them on the web which is kept up to date.

It has a good ammount of tutorials in it, but half the book is also functions. Oh well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum