php使用嵌套for循环外的变量

php使用嵌套for循环外的变量,php,mysql,Php,Mysql,嗨,这可能是一个新问题,但我不能让我的代码工作。 我试图对数据库中单元格中给出的数字求和。然后通过生成一个求和值数组使它们易于访问 让事情更清楚一点。数据库包含许多行,这些行将通过相同类型的代码。每行至少有40个单元格,我只挑选13-33号单元格。 单元格包含12个编号,每个编号为0110110005。我不想把所有的单元格相加。 只需将每个单元格中的数值相加 <?php // Get row 1 from the database and put into variable $row1

嗨,这可能是一个新问题,但我不能让我的代码工作。 我试图对数据库中单元格中给出的数字求和。然后通过生成一个求和值数组使它们易于访问

让事情更清楚一点。数据库包含许多行,这些行将通过相同类型的代码。每行至少有40个单元格,我只挑选13-33号单元格。 单元格包含12个编号,每个编号为0110110005。我不想把所有的单元格相加。 只需将每个单元格中的数值相加

<?php 

// Get row 1 from the database and put into variable $row1          
$row1 = mysql_fetch_row($result);

// create an emty variable with the name $str, for use later outside of the for loop         
$str = '';

// read in the cells with index 13 to 33.
for ($i = 13; $i < 33; ++$i) {
    // put the values in a string and add "," between the values.
    $str .= "$row1[$i],";                   
};

// Split the values into an array, useing "," as an divider.
$arr2 = ( explode( ',', $str ));

// Read in all the values from $arr2, index 0 to 20.
for ($p = 0; $p < 20; ++$p) {

    // make variable $b2 to the values that are in $arr2[index 0 to 20].
    $b2 = $arr2[$p];

    $a2 = 0;  // $a2 to 0

    // for the first 10 numbers in the given strings $arr2[index 0 till 20].
    for ($i = 0; $i < 10; ++$i) {
        // sum those 10 numbers in every string
        $a2 += $b2[$i];
    };

    // this is giving me the correct numbers like "909090900000000010230" 
    // 21 numbers that is the sum of every strings value.
    print_r($a2); 
}; 

因此,我的问题是需要能够在代码后面的许多地方回显$arr1。

基本上,如果希望变量在循环外部可见,就必须在循环外部创建它。因此,在启动for循环之前对其进行初始化

此外,数组比包含多个值的字符串更易于操作。所以我做了一些修改,使事情更容易处理

<?php 

// Get row 1 from the database and put into variable $row1          
$row1 = mysql_fetch_row($result);

// read in the cells with index 13 to 33.
$cells = array();
for ($i = 13; $i < 33; ++$i) {
    // put the values in an array
    $cells[] = $row1[$i];                   
};

// init cell total array
$cell_tot = array(); 
// Read in all the values from $arr2, index 0 to 20.
for ($p = 0; $p < 20; ++$p) {

    // make variable $b2 to the values that are in $arr2[index 0 to 20].
    $cell = $cells[$p];

    // for the first 10 numbers in the given strings $arr2[index 0 till 20].
    $cell_tot[$p] = 0;
    for ($i = 0; $i < 10; ++$i) {
        // sum those 10 numbers in every string
        $cell_tot[$p] += $cell[$i];
    };

    // this is giving me the correct numbers like "909090900000000010230" 
    // 21 numbers that is the sum of every strings value.
    echo $cell_tot[$p]; 
}; 
// cell_tot now visible outside the loop and is an array
// one occurance for each sumation process
// and contains one occurance for each sumation
// and not another string which is difficult to do anythig with
// also resolves the problem of one sumation adding up to more than a single char i.e. 1 to 9
// 10 would have caused you a problem
print_r($cell_tot);

1.mysql_u*已弃用,2。为什么不直接在SQL语句/查询中计算呢。试着用正确的方法调试。错误报告e_ALL;+ini_设置“显示错误”,为真;var_dump/print_r是你的朋友我对所有这些编码都很陌生。。。现在已经三周了。每一条建议都值得赞赏,我一直在使用$Sc2=$row1[14];$a2=0;对于$i=0$i<10++$i{$a2+=$Sc2[$i];}但是使用它我可以轻松地填充1000行代码为什么$a2与$arr1=str_split$a2;不在同一范围内;?这是不正确的——如果变量在同一个函数中,那么它们在循环中是可见的。关于变量的作用域,请参阅!这是一个很好的工作方式:D希望在某个时候,当我自己再次学到一些东西时,我想知道是否有可能通过脚本传递空值。现在Null被转换成0,如果数据库字段中没有写入任何内容,我不想要0。不确定你的意思是什么。也许最好再问一个问题。
<?php 

// Get row 1 from the database and put into variable $row1          
$row1 = mysql_fetch_row($result);

// read in the cells with index 13 to 33.
$cells = array();
for ($i = 13; $i < 33; ++$i) {
    // put the values in an array
    $cells[] = $row1[$i];                   
};

// init cell total array
$cell_tot = array(); 
// Read in all the values from $arr2, index 0 to 20.
for ($p = 0; $p < 20; ++$p) {

    // make variable $b2 to the values that are in $arr2[index 0 to 20].
    $cell = $cells[$p];

    // for the first 10 numbers in the given strings $arr2[index 0 till 20].
    $cell_tot[$p] = 0;
    for ($i = 0; $i < 10; ++$i) {
        // sum those 10 numbers in every string
        $cell_tot[$p] += $cell[$i];
    };

    // this is giving me the correct numbers like "909090900000000010230" 
    // 21 numbers that is the sum of every strings value.
    echo $cell_tot[$p]; 
}; 
// cell_tot now visible outside the loop and is an array
// one occurance for each sumation process
// and contains one occurance for each sumation
// and not another string which is difficult to do anythig with
// also resolves the problem of one sumation adding up to more than a single char i.e. 1 to 9
// 10 would have caused you a problem
print_r($cell_tot);