The idea is that a max and a minimum value are posted, and then a random number between the two is generated.
For example, $mins = array(30,40,50,60)
and $maxs = array(40,50,60,70)
, and then my code:
foreach($mins as $minkey => $maxval) {
foreach($maxs as $maxkey => $maxval) {
$hits[$maxkey] = rand($minval,$maxval);
}
}
If I then var_dump($mins)
, var_dump($maxs)
and var_dump($hits)
, I get:
array(4) { [1]=> string(2) "30" [2]=> string(2) "40" [3]=> string(2) "50" [4]=> string(2) "60" } array(4) { [1]=> string(2) "40" [2]=> string(2) "50" [3]=> string(2) "60" [4]=> string(2) "70" } array(4) { [1]=> int(27) [2]=> int(36) [3]=> int(19) [4]=> int(41) }
I thought the fact that $maxs
and $mins
contained string values, while $hits
containing integers (i.e. rand()
requires integer inputs ?) might be the issue so I updated my loop to become:
foreach($mins as $minkey => $minval) {
foreach($maxs as $maxkey => $maxval) {
$minval = (int)$minval;
$maxval = (int)$maxval;
$hits[$maxkey] = rand($minval,$maxval);
}
}
But on var_dump($mins)
, var_dump($maxs)
and var_dump($hits)
, this was dumped:
array(4) { [1]=> string(2) "30" [2]=> string(2) "40" [3]=> string(2) "50" [4]=> string(2) "60" } array(4) { [1]=> string(2) "40" [2]=> string(2) "50" [3]=> string(2) "60" [4]=> string(2) "70" } array(4) { [1]=> int(0) [2]=> int(47) [3]=> int(0) [4]=> int(55) }
Does anyone have any idea why this isn't working as expected?
Any thoughts/answers/comments would be very much appreciated :)!