2

I have an array like this $arr = [1,2,4,5,6,7,8,9] but I need a random version of this array, I was using the function shuffle like this $random = shuffle($arr) but this function just returns true and not a random version of the array.

3
  • Just print the array again you will be able to see the random version of your array. Commented Dec 7, 2016 at 7:51
  • Possible duplicate of How do I select random values from an array in PHP? Commented Dec 7, 2016 at 7:54
  • You were using the correct function shuffle but you do not need to assign the result - at least not to the same variable name, so simply $res = shuffle( $arr ); Commented Dec 7, 2016 at 8:01

2 Answers 2

3

shuffle

This function shuffles (randomizes the order of the elements in) an array. It uses a pseudo random number generator that is not suitable for cryptographic purposes.

The reason why you are getting true is because the shuffle function randomizes the elements correctly.

The array is passed by reference and not by value which mean that the next time you access the array it should be randomized

Sign up to request clarification or add additional context in comments.

Comments

1

I think you can do as this way you will get a result.

$input = [1,2,4,5,6,7,8,9]; shuffle($input); print_r($input);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.