按值切片/限制数组

按值切片/限制数组

问题描述:

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.

背景; strong> 创建一个有趣的赌博游戏的下拉菜单(学生可以' 在一个表单中下注'他们是对的)。 p>

变量; strong> $ balance 学生以3英镑开头,玩10英镑的桌子 p>

$ table(有一个; p>

£10表,范围为1,2,3等等。 p>

£100表,范围为10,20,30等100。 p>

£1,000表,范围为100,200,300,400等 1000.) p>

我已将$ table分配给最大值的零个数, eg $ table = 2; 对于100英镑的表格 p>

限制; strong> 我只希望下拉菜单提供最高的12个可能值(这可能包括下表-IMP! )。 学生不会自动被允许在“下一个”牌桌上比赛。 p>

资源; strong> an数组的可能值; p> $ 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); code> pre>

我可以写一种方法来按表限制数组; \ n(任何表的最大键是(9 * $ table))//因此为什么我使用上面的零(真正的游戏达到10亿美元!) p>

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

现在我需要一种方法来确保 $ maxbyTable中没有VALUE大于$ balance。 创建所有允许投注的$ maxBet数组。 p>

这就是我要去的地方! p> \ n

(然后我会执行“array_slice($ maxBet,-12);”来呈现 只有下拉列表中最高的12个) p>

编辑 - 我更喜欢不必使用数组散步,因为当我知道数组要在哪里结束时似乎没必要。 p >

第二次编辑道歉我意识到有一种方法可以在数学上确定哪个KEY映射到最高出价。 p>

它将如下所示 p>

  $ integerLength = strlen($ balance); // $ balance中的位数
 
 
 $ firstDigit = substr($ balance,0,1);  
  code>  pre> 
 
 

然后因为这种特殊模式而有些诡计 p>

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

例如; p>

  $ balance = 792; 
 \  n $ maxKeyValue =((3 * 9) -  10 + 7); //(键[24] = 700)
  code>  pre> 
 
 

这虽然解决了这个问题, 无法解决我的编程问题。 p> div>

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!