How to pick one or more random entries from multi-array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sb439
Forum Newbie
Posts: 4
Joined: Mon Oct 31, 2011 9:52 am

How to pick one or more random entries from multi-array

Post by sb439 »

Hi,

I've been trying to randomly select one or more entries of a multidimensional array without much success. can anyone suggest a good method? In my code i've tried using array_rand, but doesn't always work.

Code: Select all

$this->obArr = array(200=>array(200=>array('blue circle',200, 200)),
			                   100=>array(100=>array('blue triangle',100, 100)),
					   75=>array(75=>array('orange circle', 75, 75)),
					   300=>array(300=>array('orange triangle', 300, 300)),
					   10=>array(115=>array('red circle', 10, 115)),
					   25=>array(300=>array('red triangle', 25, 300)),
					   15=>array(100=>array('white square', 15, 100)));

$random_key = array_rand($this->obArr,1);
$random_key1 = array_rand($this->obArr[$random_key],1);
$this->a = array($this->obArr[$random_key][$random_key1]); 

My array ($this->obArr) could be upto 50 entries long. I would like to randomly pick 3 i.e.

Code: Select all

$this->a = array($this->obArr[100][100], $this->obArr[25][300],$this->obArr[15][100]);
Thanks SB439
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: How to pick one or more random entries from multi-array

Post by mikosiko »

In my code i've tried using array_rand, but doesn't always work.
why not?

test this code ($this eliminated just for testing purposes)

Code: Select all

<?php
 $obArr = array(200=>array(200=>array('blue circle',200, 200)),
                100=>array(100=>array('blue triangle',100, 100)),
                 75=>array(75=>array('orange circle', 75, 75)),
                300=>array(300=>array('orange triangle', 300, 300)),
                 10=>array(115=>array('red circle', 10, 115)),
                 25=>array(300=>array('red triangle', 25, 300)),
                 15=>array(100=>array('white square', 15, 100)));

// Select 2 random keys from the outer array
$random_key = array_rand($obArr,2);

// test result
var_dump($random_key);

// Select 1 random key from the inner array associated to the first random key that we get before
$random_key1 = array_rand($obArr[$random_key[0]],1);
//check the key
var_dump($random_key1);

//assign and display result
$a = array($obArr[$random_key[0]][$random_key1]);
var_dump($a); 

// Select 1 random key from the inner array associated to the second random key that we get initially
$random_key2 = array_rand($obArr[$random_key[1]],1);
// check the key
var_dump($random_key2);

//assign and display
$a = array($obArr[$random_key[1]][$random_key2]);
var_dump($a);
?>
is that what you need?
sb439
Forum Newbie
Posts: 4
Joined: Mon Oct 31, 2011 9:52 am

Re: How to pick one or more random entries from multi-array

Post by sb439 »

Yes, thank you works great!

I'm eventually going to have up to 50+ elements in my array ($obArr). I obviously need some sort of for loop to select say 5 random from the 50. Using the code above within this what would be the best approach? i.e. in need 5 elements from the array $obArr

Something this, but not complete:

Code: Select all

$req = 5;
for ($i=1; $i<=$req,$num++) {
        $random_key = array_rand($obArr,$req);
        $random_key1 = array_rand($obArr[$random_key[$i-1]],1);
	$a = array($obArr[$random_key[$i-1]][$random_key1]);	
}
return $this->a;
Hope this makes sense; struggling with this as a noob. Would i need to push or splice on $a??

Thanks SB439
Post Reply