使用PHP将while索引替换为while循环中的上一个索引

时间:2014-03-28 03:42:14

标签: php

我已经尝试过以下PHP代码:

$i=1;
foreach(getTop5($pid) as $da){
    $i++                
    if($da['index_enjeu_percu'] != NULL){
      foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
        $avg = $data["avg_index_importance"].', ';

        $poin_important = substr($avg,0,-2);
        $enjeu .= $data["index_enjeu"].', ';
        $res_enjeu = substr($enjeu,0,-1);
        echo $avg;
        }
    }
    else{
        // I want to do the foreach again in here but my data $da['index_enjeu_percu'] == NUll
        //I don want to take it as NULL value, I want to take the prevous value of $da['index_enjeu_percu']
        foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
            $avg = $data["avg_index_importance"].', ';

            $poin_important = substr($avg,0,-2);
            $enjeu .= $data["index_enjeu"].', ';
            $res_enjeu = substr($enjeu,0,-1);
            echo $avg;
        }
    }
}

我想要的是:我有如上所述的循环数据         例如:如果我的结果为:1 2 3 4 NULL如果它等于NULL我想用NULL替换4         我没有解决方案。请帮助我,谢谢

2 个答案:

答案 0 :(得分:2)

将以前的值保存到会话中,尝试如下:

session_start();
$i=1;
foreach(getTop5($pid) as $da){
    $i++;                
    if($da['index_enjeu_percu'] != NULL){
      $_SESSION['prev_value']=$da['index_enjeu_percu']; // This will save the value of da['index_enjeu_percu'] to session
      foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
        $avg = $data["avg_index_importance"].', ';

        $poin_important = substr($avg,0,-2);
        $enjeu .= $data["index_enjeu"].', ';
        $res_enjeu = substr($enjeu,0,-1);
        echo $avg;
        }
    }
    else{
        $da['index_enjeu_percu'] = $_SESSION['prev_value']; // This will get the previous value of index
        foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
            $avg = $data["avg_index_importance"].', ';

            $poin_important = substr($avg,0,-2);
            $enjeu .= $data["index_enjeu"].', ';
            $res_enjeu = substr($enjeu,0,-1);
            echo $avg;
        }
    }
}

答案 1 :(得分:0)

将最后一个值保存在变量中。检查$ avg是否为null然后打印上一个数字,否则打印$ avg。

$i=1;
$prev_avg = NULL;
foreach(getTop5($pid) as $da){
    $i++                
    if($da['index_enjeu_percu'] != NULL){
      foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
        $avg = $data["avg_index_importance"].', ';

        $poin_important = substr($avg,0,-2);
        $enjeu .= $data["index_enjeu"].', ';
        $res_enjeu = substr($enjeu,0,-1);
        if ($avg == NULL)
            echo $prev_avg;
        else
            echo $avg;
        $prev_avg = $avg;
        }
    }
    else{
        // I want to do the foreach again in here but my data $da['index_enjeu_percu'] == NUll
        //I don want to take it as NULL value, I want to take the prevous value of $da['index_enjeu_percu']
        foreach(getDataChart($pid,$da['index_enjeu_percu']) as $data){
            $avg = $data["avg_index_importance"].', ';

            $poin_important = substr($avg,0,-2);
            $enjeu .= $data["index_enjeu"].', ';
            $res_enjeu = substr($enjeu,0,-1);
        if ($avg == NULL)
            echo $prev_avg;
        else
            echo $avg;
        $prev_avg = $avg;

        }
    }
}