drrhr20884 2012-02-15 16:56
浏览 45
已采纳

按值切片/限制数组

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.

  • 写回答

1条回答 默认 最新

  • dongwang6837 2012-02-15 17:11
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)