I need to display one of two items in an array based up on a 40/60% ratio. So, 40% of the time, item one displays and 60% of the time, item two displays.
I now have the following code that will just randomly choose between the two, but need a way to add the percentage weight to it.
$items = array("item1","item2");
$result = array_rand($items, 1);
echo $items[$result];
Any help would be appreciated. Thanks!
Something like that should do the trick
$result = $items[ rand(1, 100) > 40 ? 1 : 0 ];
$val = rand(1,100);
if($val <= 40)
return $items[0];
else
return $items[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With