Add PHP Array data

0

I have the following arrangement: (this data is sorted by invoice but could be scrambled).

$datos =[{"factura":88,"total":41587.3942},{"factura":88,"total":43.01},{"factura":88,"total":1472},{"factura":88,"total":117.98},{"factura":88,"total":66.01},{"factura":999,"total":41587.3942},{"factura":999,"total":516.12},{"factura":999,"total":128},{"factura":999,"total":235.96},{"factura":999,"total":330.05}];

and I would like to add the values where the invoice is the same (in this case they are 2 invoice numbers, therefore my new arrangement wanted

Example:

$totalF =[{"factura":88,"total":41587.3942},{"factura":999,"total":42763.33}];

I hope you can help me.

    
asked by Soldier 08.12.2018 в 01:07
source

2 answers

1

I have translated your array of objects to php, since you ask that it be done in php.

    $datos = [
        ["factura"=>88,"total"=>41587.3942],
        ["factura"=>88,"total"=>43.01],
        ["factura"=>88,"total"=>1472],
        ["factura"=>88,"total"=>117.98],
        ["factura"=>88,"total"=>66.01],
        ["factura"=>999,"total"=>41587.3942],
        ["factura"=>999,"total"=>516.12],
        ["factura"=>999,"total"=>128],
        ["factura"=>999,"total"=>235.96],
        ["factura"=>999,"total"=>330.05]
        ];

    $facturas = [];
    $facturas_id = [];
    foreach($datos as $data){
        if(!in_array($data['factura'],$facturas_id)){
            $facturas[$data['factura']] = $data['total'];
            $facturas_id[] = $data['factura'];
        }
        else{
            $facturas[$data['factura']] += $data['total'];
        }
    }

    echo '<pre>';
    print_r($facturas);
    echo '</pre>';

The idea is simple:

  • We go through the array and create two more arrays before:

  • one ($ invoices_id) that will be responsible for storing the id's of the invoices. This array will only tell us if as we go through the data array, we have passed or not an invoice with that id. If that invoice id does not exist in the array, it will put it inside; but it will not do anything (it will not put it inside, I mean).

  • the other ($ invoices) will have as many indexes as there are invoice ids and their values will be what accumulates in the total as we go through the foreach loop.

  • What we do will be (as the loop is traversed): we see if the id of the invoice we are evaluating exists or not in the first of the arrays (in $ invoices_id). If it does not exist, it is understood that it is the first time that we have found that invoice id, so the total up to that moment for that id is zero, so we simply assign it the total of that particular invoice. If, on the contrary, there was the id of the invoice in the first of the arrays ($ invoices_id), what we do is add (over the second of our arrays) in that same index the total value to what it has accumulated.

Well, the explanation is a bit messy perhaps, but it works.

I hope I have helped you.

    
answered by 08.12.2018 / 01:47
source
1

It is possible to use the array_reduce () function, from php:

$totales = array_reduce($datos, function($a, $x) { 
    $a[$x['factura']] += $x['total'];
    return $a;
}, []);

This function of PHP indicates a function that works with two arguments: the partial or accumulated result and the data to iterate of those that make up the array. What array_reduce does is call your function once for each element of the array. Your function will return the partial result of combining that element with the previous result, starting from an initial result that, in this case, is an empty array.

    
answered by 08.12.2018 в 12:00