Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-16-2005, 10:29 PM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.

Last edited by Element; 11-16-2005 at 10:34 PM..
Element is offline   Reply With Quote
Old 11-16-2005, 11:11 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
... either that or array_rand().
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-17-2005, 07:25 AM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 11-17-2005, 03:16 PM   PM User | #4
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-23-2005, 12:14 AM   PM User | #5
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 11-23-2005, 01:48 PM   PM User | #6
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-24-2005, 08:05 AM   PM User | #7
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Old 11-24-2005, 03:01 PM   PM User | #8
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
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.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 11-24-2005, 08:11 PM   PM User | #9
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
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.
Element is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.