按值切片/限制数组

Background; to create a dropdown menu for a fun gambling game (Students can 'bet' how much that they are right) within a form.

Variables; $balance Students begin with £3 and play on the £10 table

$table(there is a;

£10 table, with a range of 1,2,3 etc to 10.

£100 table with a range of 10,20,30 etc to 100.

£1,000 table with a range of 100, 200, 300, 400 etc to 1000.)

I have assigned $table to equal number of zeros on max value, eg $table = 2; for the £100 table

Limitations; I only want the drop down menu to offer the highest 12 possible values (this could include the table below -IMP!). Students are NOT automatically allowed to play on the 'next' table.

resources; an array of possible values;

$a = array(1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000);

I can write a way to restrict the array by table; (the maximum key for any table is (9*$table) )//hence why i use the zeroes above (the real game goes to $1 billion!)

$arrayMaxPos = (9*$table);
$maxbyTable = array_slice($a, 0, $arrayMaxPos);

Now I need a way to make sure no VALUE in the $maxbyTable is greater than $balance. to create a $maxBet array of all allowed bets.

THIS IS WHERE I'M STUCK!

(I would then perform "array_slice($maxBet, -12);" to present only the highest 12 in the dropdown)

EDIT - I'd prefer to NOT have to use array walk because it seems unnecessary when I know where i want the array to end.

SECOND EDIT Apologies I realised that there is a way to mathematically ascertain which KEY maps to the highest possible bid.

It would be as follows

$integerLength = strlen($balance);//number of digits in $balance


$firstDigit = substr($balance, 0, 1); 

then with some trickery because of this particular pattern

$maxKeyValue = (($integerlength*9) - 10 + $firstDigit);

So for example;

$balance = 792;

$maxKeyValue = ((3*9) - 10 + 7);// (key[24] = 700)

This though works on this problem and does not solve my programming problem.

Optional!

First of all, assuming the same rule applies, you don't need the $a array to know what prices are allowed on table $n

$table = $n; //$n being an integer
for ($i = 1; $i <= 10; $i++) {
    $a[] = $i * pow(10, $n);
}

Will generate a perfectly valid array (where table #1 is 1-10, table #2 is 10-100 etc).


As for slicing it according to value, use a foreach loop and generate a new array, then stop when you hit the limit.

foreach ($a as $value) {
    if ($value > $balance) { break; }
    $allowedByTable[] = $value;
}

This will leave you with an array $allowedByTable that only has the possible bets which are lower then the user's current balance.


Important note

Even though you set what you think is right as options, never trust the user input and always validate the input on the server side. It's fairly trivial for someone to change the value in the combobox using DOM manipulation and bet on sums he's not supposed to have. Always check that the input you're getting is what you expect it to be!