Reply
need an example using php_rand on a multidimensional array please
Old 07-31-2010, 09:08 AM need an example using php_rand on a multidimensional array please
Webmaster Talker

Posts: 607
Trades: 0
Hi, I am trying to get a random monster from my multidimensional "monsters" array. I have array monsters, and inside that I have array "ocean" and inside that are "fish", "shark" etc.
like the below, and I am trying to pick out a random monster from the Ocean array. Any help greatly appreciated. thank you. Derek

here is the array

Code:
$monsters = array
(
   'Ocean'=>array
   (
      'octalisk'=>array
      (
         'name'=>'octalisk',
         'hp'=>100,
         'damageLow'=>1,
         'damageHigh'=>15,
         'exp'=>10,
         'ac'=>'HAVE TO LOOK THIS UP',
         'monsterInitiativeModifier'=>'HAVE TO LOOK THIS UP'),
    
      'shark',
      'eel'
   ),
   'Desert'=>array
   (
      'sand_snake'
   ),
   'Forest'=>array
   (
      'frog',
      'lizard',
      'spider'
   )
);
silverglade is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 07-31-2010, 09:39 AM Re: need an example using php_rand on a multidimensional array please
Webmaster Talker

Posts: 607
Trades: 0
is this right please? well, it didn't work. it only output
"the current monster is " without echoing out the value.

Code:
     $currentMonster = $monsters[array_rand($monsters['Ocean'])];
echo "the current monster is ".$currentMonster;
ok time to go to bed

Last edited by silverglade; 07-31-2010 at 10:22 AM..
silverglade is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 10:32 AM Re: need an example using php_rand on a multidimensional array please
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Do We Need Longer Passwords?
Posts: 1,358
Name: Matt
Location: Irvine, CA
Trades: 0
Just to clarify, you are trying to get a random cell from a two dimensional array?
PHP Code:
$x array_rand($monsters);
$y array_rand($monsters[$x]);

$randMonster $monsters[$x][$y]; 
__________________
Tinsology | How to Post Code | ErrorDatabase
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-31-2010, 12:25 PM Re: need an example using php_rand on a multidimensional array please
Webmaster Talker

Posts: 607
Trades: 0
thank you very much! Yes I'm trying to get random monsters from the Ocean array that is part of the Monsters array. Derek
silverglade is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 01:27 PM Re: need an example using php_rand on a multidimensional array please
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Do We Need Longer Passwords?
Posts: 1,358
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by silverglade View Post
thank you very much! Yes I'm trying to get random monsters from the Ocean array that is part of the Monsters array. Derek
In that case just specify the key:
PHP Code:
$x 'Ocean'
$y array_rand($monsters[$x]); 

$randMonster $monsters[$x][$y]; 
__________________
Tinsology | How to Post Code | ErrorDatabase
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-31-2010, 01:31 PM Re: need an example using php_rand on a multidimensional array please
Webmaster Talker

Posts: 607
Trades: 0
Great thank you for that. It outputs the random ocean monsters, but it also echoes out "array" , so it echoes out like this, "eel, shark, Array, eel, Array".
Is there anything I can do to stop that? Any more help GREATLY appreciated, thank you for the code. Derek

here is the actual array.

Code:
$monsters = array
(
    'Ocean'=>array
    (
        'octalisk'=>array
        (
            'name'=>'octalisk',
            'hp'=>100,
            'damageLow'=>1,
            'damageHigh'=>15,
            'exp'=>10,
            'ac'=>'HAVE TO LOOK THIS UP',
            'monsterInitiativeModifier'=>'HAVE TO LOOK THIS UP'),
     
        'shark',
        'eel'
    ),
    'Desert'=>array
    (
        'sand_snake'
    ),
    'Forest'=>array
    (
        'frog',
        'lizard',
        'spider'
    )
);

Last edited by silverglade; 07-31-2010 at 01:33 PM..
silverglade is offline
Reply With Quote
View Public Profile
 
Old 07-31-2010, 01:39 PM Re: need an example using php_rand on a multidimensional array please
NullPointer's Avatar
Will Code for Food

Latest Blog Post:
Do We Need Longer Passwords?
Posts: 1,358
Name: Matt
Location: Irvine, CA
Trades: 0
Since the ocean array contains both arrays and strings you can't just echo the value you $randMonster; you need to check the type first:
PHP Code:
$x 'Ocean';  
$y array_rand($monsters[$x]);  

$randMonster $monsters[$x][$y];

if(
is_array($randMonster))
     echo 
$randMonster['name'];
else
     echo 
$randMonster
__________________
Tinsology | How to Post Code | ErrorDatabase
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to need an example using php_rand on a multidimensional array please
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13848 seconds with 13 queries