Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

How would I...

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

Barrucadu

Established Member
Impact
64
Get 5 random values from an array?
And display them in alphabetical order.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
asort(array_rand($myarray, 5)); // for sorting values
ksort(array_rand($myarray, 5)); // for sorting keys

You can use sort() but it rewrites the keys
 
Last edited:
0
Close
PHP:
asort(array_rand($videos, 5));

just returns the first 5 videos (ie: not choosing 5 random ones)
 
0
Close
dunno, the function is array_rand, i use it quite alot.

try it without the sorts

PHP:
$arr = array('dog','cat','cow','horse','fish');
$rnd =(array_rand($arr,2));
echo $arr[$rnd[0]] . "<br/>";
echo $arr[$rnd[1]] . "<br/>";

then try this
PHP:
$arr = array('dog','cat','cow','horse','fish');
$rnd =(array_rand($arr,2));
foreach($rnd as $a){
	$temp[] = $arr[$a];
}
asort($temp);
print_r($temp);
 
Last edited:
0
Close
Thanks, working now
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back