phprookie72 Posted August 28, 2012 Share Posted August 28, 2012 would like to know how i can tell array_rand not to select a specific elements from an array? Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/ Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 Remove them from the array. Could you please be more specific? You could always shuffle, and then loop until a value that isn't one of the 'bad' elements is found. Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/#findComment-1373267 Share on other sites More sharing options...
phprookie72 Posted August 28, 2012 Author Share Posted August 28, 2012 array_rand function select 2 random elements from an array, is it possible to tell the function not to select a certain elements? Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/#findComment-1373269 Share on other sites More sharing options...
xyph Posted August 28, 2012 Share Posted August 28, 2012 No. You have to program that functionality in manually. I thought I was very straightforward in my first response Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/#findComment-1373276 Share on other sites More sharing options...
Psycho Posted August 28, 2012 Share Posted August 28, 2012 hphrookie72, your requests are very vague and it makes it difficult for us to help you. As xyph stated if you don't want array_rand() to find specific items in an array - remove them. That is how you tell array_rand() not to return them. This kinda sounds like it could be a homework problem, so I'm not going to write any code for you. But, if you post some code you've written and need some help, we can push you in the right direction. But, it all depends on HOW you want to limit the results from array_rand(). Do you know the exact values you want excluded? If so, look into array_diff(). IF you are wanting to exclude elements that 'contain' something in the value you're going to have to write a custom procedure to do that. Either a recursive process to continually get a value using array_rand() that matches your criteria OR preprocess the values in the array to remove ones that you don't want returned. Which one would depend on the size of the array and how many options would not apply. Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/#findComment-1373290 Share on other sites More sharing options...
Barand Posted August 28, 2012 Share Posted August 28, 2012 You may also want to look at array_filter and use that prior to array_rand() Quote Link to comment https://forums.phpfreaks.com/topic/267707-array_rand-function/#findComment-1373330 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.