doujie1908 2014-10-14 20:13
浏览 22
已采纳

PHP将变量除以多个多维数组以不产生余数

The project I have is a reimbursement of materials in a game. The materials come in various packages listed in the array below. Part of my code gives me the total number of units I need to reimburse, and I have to use the "packages" from the array below.

This is the multi dimensional array. The $item['ore'][100] array indicates that the item listed gives 100 units when used and stacks to only 100 (key => stacksize). Likewise for $item['ore'][500] gives 500 units when used and only stacks to 100:

$item = array(
 'ore'  =>  array(
    100 => array(
        'name'  => "Item 1",
        'stacksize' => 100,
    ),
    500 => array(
        'name'  =>  "Item 2",
        'stacksize' => 100,
    ),
    1000    =>  array(
        'name'  => "Item 3",
        'stacksize' => 100,
    ),
),

So, 1x Item 1 gives 100 units, 1x Item 2 gives 500 units and 1x Item 3 gives 1000 units. My $totalOre variable from part of my code dictates that I have 15,825 units to reimburse total (as an example). Obviously I'll have some extra that will be reimbursed as I don't have an item that gives smaller than 100 units (not a big deal if the user gets a bit more in this case).

So I need a code snippet to tell me that I need to reimburse 15x Item 3, 1x Item 2 and 4x Item 1 for a total of 15,900 units.

Additionally, if I have say....$totalOre = 219,427, I need my code to dictate that I send 100x Item 3, then 100x Item 3, then 19x Item 3, then 1x Item 2 for a total of 219,500 units, because of the 'stacksize' limitation of 100 (I can't send a user more than 100x of these items or they won't get them).

I assume some sort of loop would be needed in addition the Modulus (%) operator. I tried

$reimburseOre = 0;
$reimburseOre = $reimburseOre % $items['ore'][1000];

and came up with 0. Any assistance you can provide would be greatly appreciated, even just a hand in the right direction.

Thank you.

EDIT: Tried adding a 'Qty' key to my array (set to 1000) and then the following snippet of code and the result was -523 (not correct at all):

    $oreReimbursement = 0;
    if ($totalOre % $items['ore'][1000]['Qty'] != 0) {
        $oreReimbursement -= $totalOre % $items['ore'][1000]['Qty'];
    }
    echo $oreReimbursement; ?>
  • 写回答

2条回答 默认 最新

  • doushoubu5360 2014-10-14 21:53
    关注

    Here's some code to get you on your way. You'll have to work out the stacks, but this should help with knowing what combination of ore lots you have to repay.

    # $target is the amount of ore to be repaid
    $target = 481726;
    
    # first, reverse sort the keys of 'ore' so they're in descending numerical order
    krsort( $item['ore'] );
    # initialise an array to contain our "order" of ore.
    $order = [];
    
    # for every value of ore, starting with the largest amount
    foreach ( $item['ore'] as $a => $v ) {
        # if the target to be reached is larger than the amount, $a
        if ($target > $a) {
            # floor( $target / $a ) means round down $target/$a to the nearest integer
            # add the name (Item x) and the rounded down number to the order.
            $order[ $v["name"] ] = floor($target / $a);
            # reset $target to the remainder
            $target = $target % $a;
        }
    }
    
    # if we still have some ore left to give, add one lot of the smallest ore quantity
    if ($target) {
        ( isset($order[ $v["name"] ]) )
        ? $order[ $v["name"] ]++
        : $order[ $v["name"] ] = 1;
    }
    
    print_r($order);
    

    Output for 481726:

    Array
    (
        [Item 3] => 481
        [Item 2] => 1
        [Item 1] => 3
    )
    

    You should be able to work out how to divide the 485 stacks into sets of 100.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办