Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    ... 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.

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #9
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •