Home  >  Q&A  >  body text

javascript - assign the value inside the foreach loop to the variable outside the loop or output it outside the loop

foreach ($doorUserArray as $k =>$v) {

        if (strpos($v, $need) !== false) {
           echo "'$k' => '$v'<br />";
            
        }
    }
    echo $v;//无效
    想把$v的值拿出来 该怎么做
曾经蜡笔没有小新曾经蜡笔没有小新2672 days ago1145

reply all(3)I'll reply

  • 世界只因有你

    世界只因有你2017-06-26 10:51:18

    According to the principle of program execution from top to bottom, declare a variable before foreach, and then assign the value directly inside the foreach loop. If you have any questions, you can reply at any time.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-26 10:51:18

    Set a global variable and assign the value of $v to the global variable.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-26 10:51:18

    Scope issue, before looping, set a variable externally, or extract it and make it a function and return the result

    function xyz(){
        ...
        return $v;
    }
    
    var toEcho = xyz();
    echo toEcho;

    reply
    0
  • Cancelreply