PHP Combine data from several arrays

0

I have the following case, which I can not make it work. It only works with one data for each variable. I understand that I must associate the variables, but I do not know how to do it. Any suggestions? Thank you very much!

<?php

$TotalValores=700;
$Saldo=array(100,300,200);
$Comprobantes=array(5548,5549,5550); 
$MontoAPagar=array(80,300,200);

$CantComprobantes=count($Comprobantes);


echo 'Total de Dinero: '.$TotalValores.'<br><br>';
 
for ($row = 0; $row < $CantComprobantes; $row++)
{

echo 'Saldo de factura: '.$Saldo.'<br>';

 if ($MontoAPagar >= $Saldo){
     $SaldoDeFactura = $Saldo - $MontoAPagar;
	 $TotalValores = $TotalValores - $MontoAPagar;
      
     echo 'Monto pagado: '.$MontoAPagar.'<br>';
     echo 'Saldo despues del pago: '.$SaldoDeFactura.' <br>';
     echo 'Factura PAGADA.<br>';
     echo 'Valores restante: '.$TotalValores.'<br>';
}else{
    $SaldoDeFactura = $Saldo - $MontoAPagar;
	$Pagado = $MontoAPagar - $SaldoDeFactura;
    $TotalValores = $TotalValores - $MontoAPagar;

    echo 'Monto pagado: '.$MontoAPagar.'<br>'; 
    echo 'Saldo despues del pago: '.$SaldoDeFactura.' <br>';
    echo 'Factura IMPAGA<br>'; 
    echo 'Dinero restante: '.$TotalValores.'<br>';	 
    
}	 
}

?>

This

    
asked by pointup 19.11.2018 в 14:38
source

1 answer

1

With the code as you have it, being sure that $Saldo , $Comprobantes and $MontoAPagar always contain the same number of elements, you must specify what element you are working with inside the loop, putting $row in brackets :

echo 'Saldo de factura: ' . $Saldo[$row] . '<br>';

And the same for $Comprobantes and $MontoAPagar .

Having said that, I would change the code so that the function from which you get this data will return a structure like this:

$datos = array(
    'TotalValores' => 700,
    'Facturas' => array (
        array (
            'Saldo' => 100,
            'Comprobante' => 5548,
            'MontoAPagar' => 80,
        ),
        // dos arrays más para las otras dos facturas
    )
);
    
answered by 19.11.2018 / 14:49
source