Jump to content

array_rand


Obi1-Cannabis

Recommended Posts

i have an four fields array: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) . and i'm trying to get arandom value from this array using $key = array_rand($arr);and it is working but the last value, however, is allways ignored. in this case the value 4 of field 3 is never selected but if i insert a new field 4 with a value 5. the field 3 works but not the field 4... why is this happenig?

Link to comment
Share on other sites

Hi.. Thats depends on loginc of array_rand()instead of that you can make ur own user define function..or array_rand($arr,2) so U'll get probably that..Suppose this will help U..Regards,Vijay

Link to comment
Share on other sites

Thats depends on loginc of array_rand()instead of that you can make ur own user define function..or array_rand($arr,2) so U'll get probably that..
didn't get it... the idea is not return two values...i made it work adding an extra field with the value of zero and now its working ok... but still i would like to know why is array_rand ignoring the last field, wasn't it suposed to return a random field of the intire array?
Link to comment
Share on other sites

It is working fine. If you read the reference page for array_rand, you will notice that it returns a key, not a value. In the example above, it will return 0-3. For this array:array(0 => "element 1", 1 => "another element", "test" => "something else", "more" => "even more");If you use array_rand it will return one of these values:0, 1, "test", "more"It returns the key, not the value. You can use the key to get the value.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...