d-_-b
d-_-b

Reputation: 23161

array_rand() is returning integers from my indexed array instead of the values

I'm having trouble pulling a random array value using PHP's array_rand() function. What am I doing wrong?!

$topics = array("1", "2", "3");
$colors = array("#ff3333", "#ffcc00", "#ccff33");

foreach ($topics as $t) {
    $c = array_rand($colors, 1);
    echo "style='color:$c'";
}

I'm getting integers returned, rather than my values.

Upvotes: 0

Views: 623

Answers (1)

xdazz
xdazz

Reputation: 160833

array_rand() returns the key for a random entry.

So you need to use $colors[$c].

Code:

echo "style='color:$colors[$c]'";

Upvotes: 3

Related Questions