Jump to content

[SOLVED] Selecting random values from arrays


ecopetition

Recommended Posts

Hello, I was wondering if anyone can help me with the following array:

 

array(

'1' => '1',

'2' => '2',

'3' => '3',

'4' => '4',

'5' => '5',

'6' => '6',

'7' => '7',

'8' => '8',

'9' => '9',

'10' => '10',

'11' => '11',

'12' => '12',

'13' => '13',

'14' => '14',

'15' => '15',

'16' => '16',

'17' => '17',

'18' => '18',

'19' => '19',

'20' => '20',

'21' => '21',

'22' => '22',

'23' => '23',

'24' => '24',

'25' => '25',

)

 

Now, what I want to do is select a specified number of random values from this array, say 10, and arrange them like so:

 

5, 8, 12, 13, 15, 18, 19, 21, 23, 25 (where a comma is the delimiter). The fact that the numbers are in order is not important but it is important that no number is repeated.

 

Also, every time the script is refreshed, it is important that the random values would be reselected.

 

I'd appreciate any help with this at all.

 

Thanks,

Peter

Thanks.

 

Now what if I was to do it so that the array was made of text rather than numbers, eg:

 

array(

'1'  =>  'cheese',

'2'  =>  'chocolate',

'3'  =>  'butter',

'4'  =>  'milk',

'5'  =>  'grapes',

'6'  =>  'bread',

'7'  =>  'wheat',

'8'  =>  'rice',

'9'  =>  'spice',

 

[etc]

 

)

<?php
$stuff = array(
'1'   =>   'cheese',
'2'   =>   'chocolate',
'3'   =>   'butter',
'4'   =>   'milk',
'5'   =>   'grapes',
'6'   =>   'bread',
'7'   =>   'wheat',
'8'   =>   'rice',
'9'   =>   'spice'
);
$rand_key = array_rand($stuff);
$rand_element = $stuff[$rand_key];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.