douti0687 2015-12-10 22:05
浏览 51

在php中填充数组时索引出错

The first step is to create an new array with zeros. This is the code:

$amounts = [];
    $row = [];
    for($a = 0; $a < count($receipts_with_total); $a++){
        for($b = 0; $b < count($taxes); $b++){
            $row[$b] = 0;               
        }
        $amounts[] = $row;
    }    

Then, i proceede to fill the array with values. The problem is, for some reason i don't know, it adds some indexs. The code to fill the array is the next one:

//We calculate all the taxes amounts        
    for($i = 0; $i < count($receipts_with_total); $i++){
        $this_receipt = $receipts_with_total[$i];
        //We get all the taxes for each receipt
        $taxes = $this_receipt->taxes;
        for($j = 0; $j < count($taxes); $j++){
            $this_tax = $taxes[$j];             

            if($this_tax->name == "IVA 21%"){
                $amounts[$i][$j] = round((($this_tax->value * $total[$i]) / 100), 2);
            }
            elseif($this_tax->name == "IVA 10.5%"){
                $amounts[$i][$j+1] = round((($this_tax->value * $total[$i]) / 100), 2);
            }
            else {
                $amounts[$i][$j+2] = round((($this_tax->value * $total[$i]) / 100), 2); 
            }           
        }
    }    

And the outputs are:

Creacion

Array ( [0] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [1] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [2] => Array ( [0] => 0 [1] => 0 [2] => 0 ) [3] => Array ( [0] => 0 [1] => 0 [2] => 0 ) )

Modelo

Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.36 [2] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 40.36 [2] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 105.53 [2] => 0 ) )

Lleno

Array ( [0] => Array ( [0] => 0 [1] => 257.46 [2] => 0 [3] => 61.3 ) [1] => Array ( [0] => 0 [1] => 40.37 [2] => 0 [3] => 9.61 ) [2] => Array ( [0] => 80.73 [1] => 0 [2] => 40.37 [4] => 9.61 ) [3] => Array ( [0] => 211.05 [1] => 0 [2] => 105.53 ) )

The first output is the new array with zeros. The second one is an example of as should be the final array with the calculate numbers. The last one is the array really i get. As you can see, the index in bold represent the errors. For example, the value "61.3" is in fourth position in the first array, instead of third, it would be the correct.

Thanks!

  • 写回答

2条回答 默认 最新

  • duangewu5234 2015-12-10 22:12
    关注

    Remove the +1 and +2 from the code. Just

    $amounts[$i][$j]=...
    

    in all cases.

    Because if i.e.

    $j=2;
    

    it may be become 3 in your code $j+1

    My answer just pick that part of your question:

    The problem is, for some reason i don't know, it adds some indexs.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类