2011-11-18 39 views
0

我创建下面的循环回路帮助在PHP

$x=1; 
while($x<=$excel->sheets[0]['numRows']) 
{ 
    $y=1; 
    while($y<=$excel->sheets[0]['numCols']) 
    { 
    isset($excel->sheets[0]['cells'][$x][$y])? $excel->sheets[0]['cells'] [$x][$y] : ''; 
    $y++; 

    $a1 = $excel->sheets[0]['cells'][$x][3]; 
    $a2 = $excel->sheets[0]['cells'][$x][4]; 
    $a3 = $excel->sheets[0]['cells'][$x][5]; 
    //some code to store above variables a1, a2, a3 and give the output 
    } 
    $x++; 
} 

的代码的说明:

上述代码读取excel工作表,并且存储的值和工作原理是这样,例如,电池以行5和C列将使用符号$ obj-> sheets [0] ['cells'] [5] [3]来访问。在excel中,每一行对应一条记录和每列相关的元数据。

现在我试图做的是将相应记录的所有元数据上传为一个迭代,但上面的代码将每行和每列视为一条记录,而不是将整行视为一条记录。

我在上面的代码中做了什么错误?

+0

你不需要'while($ y <...)'loop – Peter

+0

什么是变量$ cell?这不是在任何地方使用? – Bytemain

回答

0
$x=1; 
while($x<=$excel->sheets[0]['numRows']) 
{ 
    $y=1; 
    while($y<=$excel->sheets[0]['numCols']) 
    { 
    $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'] [$x][$y] : ''; 
    //some code to store $cell variable and give the output 
    $y++; 
    } 
    $x++; 
} 

或...

$x=1; 
while($x<=$excel->sheets[0]['numRows']) 
{ 
    $a1 = $excel->sheets[0]['cells'][$x][3] ; 
    $a2 = $excel->sheets[0]['cells'][$x][4]; 
    $a3 = $excel->sheets[0]['cells'][$x][5]; 
    //some code to store above variables a1, a2 , a3 and give the output 
} 
0

你抓列3,4,5 numCols倍。也许

$a1 = $excel->sheets[0]['cells'][$x][3] ; 
$a2 = $excel->sheets[0]['cells'][$x][4]; 
$a3 = $excel->sheets[0]['cells'][$x][5]; 

应该在外层循环中。