简体   繁体   中英

bidimentional array is not printing values

Need help with this array, everything works fine excpt the last line im not getting the values on $data[5][1] + $data[1][2] - $data[2][2] ,

$data = array(
    array("Concepto","Enero","Febrero","Marzo","Abril"),
    array("Ingresos",100,       100,    100,    100),
    array("Egresos",200,        200,    200,    200),
    array("Deudores",300,       300,    300,    300),
);

$aa = array("Saldo",($data[1][1]-$data[2][1]),($data[1][2]-$data[2][2]),($data[1][3]-$data[2][3]),$data[1][4]-$data[2][4]);
array_push($data,$aa);
$bb = array("Saldo Acumulado",(5000)+($data[1][1])-$data[2][1],
            $data[5][1] + $data[1][2] - $data[2][2],
            "3",
            "4");
array_push($data,$bb);

$tblCuatrimestre1 = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
foreach($data as $dat){
      $tblCuatrimestre1 .= ' <tr>
        <td width="20%">'.$dat[0].'</td>
        <td width="20%">'.$dat[1].'</td>
        <td width="20%">'.$dat[2].'</td>
        <td width="20%">'.$dat[3].'</td>
        <td width="20%">'.$dat[4].'</td>
        <td width="20%">'.$dat[5].'</td>
      </tr>';
}
$tblCuatrimestre1 .= '</table>';
echo $tblCuatrimestre1;

The problem is next:

$data = array(
        array("Concepto","Enero","Febrero","Marzo","Abril"),
        array("Ingresos",100,       100,    100,    100),
        array("Egresos",200,        200,    200,    200),
        array("Deudores",300,       300,    300,    300),
        );
        $aa = array("Saldo",($data[1][1]-$data[2][1]),($data[1][2]-$data[2][2]),($data[1][3]-$data[2][3]),$data[1][4]-$data[2][4]);
        array_push($data,$aa);

Now your $data array have 5 sub-arrays, and when you call $data[5][1] will be an error because $data[4] will be last sub-array. Probably you need

$bb = array("Saldo Acumulado",(5000)+($data[1][1])-$data[2][1],
            $data[4][1] + $data[1][2] - $data[2][2],
            "3",
            "4");
array_push($data,$bb) ;

Given comments, it becomes obvious where you went wrong with your math. But before I get to that.. normally arrays are zero-indexed.. meaning the first item starts at zero, and it counts up from there. Thus, your $data[5][1] + $data[1][2] - $data[2][2] would error out, given there is no such element in the given array.

Secondly, to your main question....

The math done on

$data[4][1] + $data[1][2] - $data[2][2]
 //   ^ correct digit here by the way

Would never equal the number you state it would equal, given the values of your array. However, given your question, I'm going to assume you're actually talking about this bit of math:

(5000)+($data[1][1])-$data[2][1]

You said the above should equal 4800, but mathematically, this is impossible. Later, in the comments, you state that your math starts with the number 4900.. and thus the error becomes clear. Change the 5000 to 4900, and it should work according to your design. Of course, you should also fix how your array is accessed, given there is no 5 index(it would be 4, and here I am repeating myself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM