dongxu1029 2014-03-28 03:42
浏览 134
已采纳

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

I have tried the PHP code as below:

$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;
        }
    }
}

What I want is that : I have loop data as above Ex: if my result is that: 1 2 3 4 NULL in case it equal NULL I want to replace NULL with 4 I don have the solution for this. Any one help me please, thanks

  • 写回答

2条回答 默认 最新

  • douxi3977 2014-03-28 03:59
    关注

    Save the previous value to a session try like this:

    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条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料