php - How randomized array keys with rand() works?
I'm a bit confused about how this works (the title)? For example, i'm setting an array like this
$array[rand(0, 5)] = 'Alex'; $array[rand(0, 5)] = 'Blue'; $array[rand(0, 5)] = 'Dale'; $array[rand(0, 5)] = 'Matt';
An example of a possible result after a
var_dump($array); of the array :
Array (  => Blue  => Dale  => Matt )
What happens technically?
your constructing array with random key for each value .rand(0,5) means it take any one number between 0-5 but may be key overwriting issue is here . its the best example . run this one more than one time and know the each output .you can see the key repeating
Consider the example below :
We have the output as below without any change :
When you use
rand(0, 5)it takes random value given , hence it keeps changing each time you run and you have different outputs.
var_dump— Dumps information about a variable
More info on var_dump