I have an array, such like:
$hex = array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
I want to return 6 random elements as a string (eg. 1a3564):
$random_color = array_rand($hex,6);
I thought imploding $random_color would do the trick:
echo implode($random_color);
But array_rand() stores positions of elements in parent array, not this array elements, so I get something like:
259111213
instead of 259bcd
.
I know this does exactly what I want:
echo $hex[$random_color[0]];
echo $hex[$random_color[1]];
echo $hex[$random_color[2]];
echo $hex[$random_color[3]];
echo $hex[$random_color[4]];
echo $hex[$random_color[5]];
But:
is there any way to store array elements within array_rand()? Why it stores elements' positions instead of elements in the first place?
what's the best way to do what I want to achieve?
why does array_rand() NEVER choose a letter as the first element, and almost never as the second/third (99% of generated colors look like 11111a 12345c 123456)?