CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Post a PHP snippet (http://www.codingforums.com/forumdisplay.php?f=41)
-   -   Random Array Key (http://www.codingforums.com/showthread.php?t=72706)

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:

marek_mar 11-16-2005 11:11 PM

... either that or array_rand().

Element 11-17-2005 07:25 AM

Quote:

Originally Posted by marek_mar
... either that or 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 03:16 PM

Quote:

Originally Posted by Element
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 12:14 AM

Quote:

Originally Posted by marek_mar
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 01: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 08:05 AM

Quote:

Originally Posted by marek_mar
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 03: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 08:11 PM

Quote:

Originally Posted by marek_mar
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.


All times are GMT +1. The time now is 03:01 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.