简体   繁体   English

双向数组未打印值

[英]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[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. 现在,您的$data数组具有5个子数组,当您调用$data[5][1]将出错,因为$data[4]将是最后一个子数组。 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. 因此,给定数组中没有这样的元素,您的$data[5][1] + $data[1][2] - $data[2][2]会出错。

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. 您说上述应该等于4800,但是从数学上讲,这是不可能的。 Later, in the comments, you state that your math starts with the number 4900.. and thus the error becomes clear. 稍后,在注释中,您声明数学以数字4900 ..开头,因此错误变得很明显。 Change the 5000 to 4900, and it should work according to your design. 将5000更改为4900,它将根据您的设计工作。 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). 当然,鉴于没有5索引(它将是4,因此我在这里重复一下),您还应该修复如何访问数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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