0

My code:

$input = array("1", "2", "3", "4", "5", "6", "8", "15", "22");
$value1=$input[array_rand($input)];

I want to randomize given numbers and get just one numbers of them back to "$input", but I got an error : "PHP Warning: array_rand() expects parameter 1 to be array, boolean given in...

Line where this error is triggered is here:

$value1=$input[array_rand($input)];

How to fix that error ?

?

Thanks!

Azem Hajdari
  • 23
  • 1
  • 7
  • 2
    ..because `$input`is of type boolean, not an array. [RTM](http://php.net/manual/en/function.array-rand.php) – Jeff May 28 '16 at 23:58
  • since we don't know what $input is, or where it comes from, we can't really help. Fact is, $input is not an array. – Jeff May 28 '16 at 23:59
  • @Jeff Oh, that's great, thanks very much for that information, so what to do to not read this error on my server over and over again ? – Azem Hajdari May 28 '16 at 23:59
  • Are you serious? Did you even read the error message? It could not be any clearer. – Sverri M. Olsen May 29 '16 at 00:00
  • @Sverri M. Olsen I am sorry, but I have no idea what to do to fix that. – Azem Hajdari May 29 '16 at 00:01
  • @Jeff Do that mean I have just to change '$input' to '$somethingelse' ?? – Azem Hajdari May 29 '16 at 00:04
  • @AzemHajdari What to do not to read the error? _FIX THE ERROR_. Show more code (where $input comes from) or Read the Manual. – Jeff May 29 '16 at 00:05
  • @Jeff With "What to do?" I already mean "How to FIX?" More code: $input = array("1", "2", "3", "4, "5", "6"); – Azem Hajdari May 29 '16 at 00:07
  • Whatever you want to pass to array_rand() has to be an array (otherwise it doesn't make sense). Please Edit your question with the whole code. Even the oneline in comments is not enough. – Jeff May 29 '16 at 00:09
  • @AzemHajdari turn on error reporting / show all errors in your development environment. You have a syntax error in the code you pasted for setting the $input variable – JimL May 29 '16 at 00:09
  • @JimL syntax error here in this line? $input = array("1", "2", "3", "4", "5", "6", "8", "15", "22"); – Azem Hajdari May 29 '16 at 00:16
  • OKAY: Now I am EDITED my question, because it seems you want all tell me WHY I am get that error and nobody provide me any solution HOW to FIX error, so now it does NOT interested me WHY but HOW TO FIX it? Thank you all, specialists. ;-) – Azem Hajdari May 29 '16 at 00:20
  • Not the right moment to be pissed off. We're trying to help, and we got a bit of experience in that. Finding out WHY you get the error is the only way to FIX the error. – Jeff May 29 '16 at 00:24
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – miken32 May 29 '16 at 02:33

3 Answers3

2

This should work:

$input = ["1", "2", "3", "4", "5", "6", "8", "15", "22"];

$randomInputIndex = rand(0, count($input)); // Returns any integer between 0 and 8 in your case
$randomInputValue = $input[$randomInputIndex];
// $input[0] returns 1
// $input[1] returns 2
// $input[7] returns 15
// $input[8] returns 22

Or simply use:

$input = ["1", "2", "3", "4", "5", "6", "8", "15", "22"];

$randomInputValue = array_rand($input);

Read more about PHP's arrays here.

JasonK
  • 5,214
  • 9
  • 33
  • 61
  • I am think that something is like that, because if I remember correctly same types of "boolean" error I am getting when I edited my code from "mysql" to "mysqli"... I am sure that will work! Many thanks JasonK ! – Azem Hajdari May 29 '16 at 00:22
  • I need $input to be just 1 given RANDOM number from those : "1", "2", "3", "4", "5", "6", "8", "15", "22" – Azem Hajdari May 29 '16 at 00:24
  • 1
    Both examples should do exactly that... have you tried them out already? – JasonK May 29 '16 at 00:27
  • that second version will work for me I guess, There is not needed to count, but important is just to give me back randomly one number from those numbers given: "1", "2", "3", "4", "5", "6", "8", "15", "22" etc. Thank you so much mr. JasonK! – Azem Hajdari May 29 '16 at 00:28
  • No problem, please accept the answer if it solved your problem. Good luck. – JasonK May 29 '16 at 00:31
  • I've accepted JasonK, thank you a million times again! Best regards! – Azem Hajdari May 29 '16 at 00:32
1

Just the way you need it!

$input = array("preto", "vermelho", "laranja", "roxo", "abacate", "pera", "uva");


$random = array_rand($input, 1);
print_r($input[$random]);

Result random unique:

laranja

Larry
  • 11
  • 2
0
$value1= $input->random(number);

ps: number = number of element you want to get ,

Arian Fm
  • 314
  • 4
  • 14