php计数器正在重置每个循环

时间:2011-09-27 17:34:31

标签: php loops foreach while-loop counter

我有一组相当复杂的循环,它将数据从mysql中提取出来,并将其与数组中的值进行比较并递增计数器。当我在计数器递增时回显一个标志时,我得到了一个bijilion标志(从mysql查询中返回了2600条记录)。但每次打印时,计数器总是1,当我在结束时打印计数器的值时,它显示为零。似乎有些东西正在重新设置柜台......

# ARRAY
$demographics=array(
    "region"=>array(
        "Northeast"=>array('total'=>0,'consented'=>0,'completed'=>0),
         //more...
        "West"=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),"societal envirn"=>array(
        "Urban"=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),"age"=>array(
        '18-19'=>array('total'=>0,'consented'=>0,'completed'=>0),
        '20-24'=>array('total'=>0,'consented'=>0,'completed'=>0),
         //more...
        '55-59'=>array('total'=>0,'consented'=>0,'completed'=>0)
    ),
    //more...
);

# LOOPS
while ($dbrecord = mysql_fetch_assoc($surveydata)) {
    foreach ( $dbrecord as $dbfield=>$dbcellval ) {
        foreach ( $demographics as $demographic=>$options ) {
            foreach ( $options as $option=>&$counter ) {
                if($demographic==="age"){
                    list($min,$max) = explode('-', $option);
                    if ($dbcellval >= $min && $dbcellval <= $max){
                        $counter['total']++;
                        echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
                        if ($dbrecord['consent']==="1"){
                            $counter['consented']++;
                            echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
                            if ($dbrecord['completion status']==="complete") {
                                $counter['completed']++;
                                echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
                                break 3;
                            } // if
                        } // if
                        break 2;
                    }
                } // if age
                else if ($option===$dbcellval){
                    $counter['total']++;
                    echo '$' . $option . "['total'] = " . $counter['total'] . "<br />";
                    if ($dbrecord['consent']==="1"){
                        $counter['consented']++;
                        echo '$' . $option . "['consented'] = " . $counter['consented'] . "<br />";
                        if ($dbrecord['completion status']==="complete") {
                            $counter['completed']++;
                            echo '$' . $option . "['completed'] = " . $counter['completed'] . "<br />";
                            break 3;
                        } // if
                    } // if
                    break 2;
                } // else if $option==$dbcellval
            } // foreach $options
        } // foreach $demographics
    } // foreach $dbrecord
} // while

示例输出

$40-44['total'] = 1
$White['total'] = 1
$35-39['total'] = 1
$Northeast['total'] = 1     // the 'total' counter is 1
$Northeast['consented'] = 1
$Northeast['completed'] = 1
$South['total'] = 1
$Northeast['total'] = 1     // notice the 'total' counter is 1 again :(
$Northeast['consented'] = 1
$Northeast['completed'] = 1

3 个答案:

答案 0 :(得分:2)

您正在使用foreach指令定义计数器,$value中的foreach($foo as $key=>$value),使用foreach时,您只有$counter的本地副本。

您需要使用foreach($foo as $key=>&$value)或从$demographics引用您的计数器的完整数组路径。

答案 1 :(得分:1)

您需要在每个级别引用您的数组,否则您正在处理数据的副本:

foreach ( $dbrecord as $dbfield=>$dbcellval ) {
    foreach ( $demographics as $demographic => &$options ) {
        foreach ( $options as $option => &$counter ) {
            if($demographic==="age"){
                list($min,$max) = explode('-', $option);
                if ($dbcellval >= $min && $dbcellval <= $max){
                    $counter['total']++;

答案 2 :(得分:-2)

如果你只是坚持使用$ demographics变量怎么办。

...
foreach ( $options as $option ) {
   ...
   $demographics[$option]['total']++;

...