$first = 1.22;
$second = 1.55;
$third = 1.77;
$fourth = 1.3;
$fifth = 1.5;
$sixth = 1.2;
$seventh = 1.4;
$eighth = 1.8;
$ninghth = 1.9;
$tenth = 1.24;
$array = array($first, $second, $third,$fourth, $fifth, $sixth, $seventh, $eighth, $ninghth, $tenth);
echo $array[mt_rand(0, count($array) - 1)]*$array[mt_rand(0, count($array) - 1)];
This works but i need it to show me what variables were multiplied and also, assign a text value on them.
CodePudding user response:
In that case you should do it in two steps, something like that:
$array = array($first, $second, $third,$fourth, $fifth, $sixth, $seventh, $eighth, $ninghth, $tenth);
$firstValue = $array[mt_rand(0, count($array) - 1)];
$secondValue = $array[mt_rand(0, count($array) - 1)];
echo "multiplying: " . $firstValue . " and " . $secondValue;
echo $firstValue*$secondvalue;
CodePudding user response:
If you have a multi dimensional array where the text is the first item and the value the second item and you want to show which text and value are multiplied, you might use for example:
$games = [["First", 1.14], ["2nd", 1.18], ["3rd", 1.22]];
$rand1 = array_rand($games);
$rand2 = array_rand($games);
$val1 = $games[$rand1][1];
$val2 = $games[$rand2][1];
echo sprintf(
"The product of '%s' (%f) and '%s' (%f) is %f",
$games[$rand1][0], $val1, $games[$rand2][0], $val2, $val1 * $val2
);
The output has the form of
The product of '2nd' (1.180000) and 'First' (1.140000) is 1.345200
See a PHP demo.