在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!

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.

I guess you want to show the "IVA 21" always in the 0 index in subarray and "IVA 10.5" always in the 1 index in sub array, and so on...? So you don't have to +1 or +2 in the index...cuz $j has already been increment in the for loop...

Or if you don't know which comes first or maybe you will have more option later, do not use a for loop. Use php foreach and keep +1 manually

$j = 0;
foreach ($taxes as $$this_tax) {
    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);
    }
   //myabe +3 later...
}

Or why not just use a static number like 0,1,2 if you always know the length of $taxes and where you gonna put for your results. You can even create conts like:

define('IVA21', 0); // const IVA21 = 0;
define('IVA105', 1);
// ... more define

//for loop starts
if ($this_tax->name == 'IVA 21%') {
    $amounts[$i][IVA21] = round((($this_tax->value * $total[$i]) / 100), 2);
}