繁体   English   中英

如何在PHP中遍历数组

[英]How to traverse an Array in PHP

我有一个像这样的数组

$steps = explode("[;;]",$str);

因此,$ steps保留了我必须用来显示PHP中的步骤过程的值。

数组看起来像这样

$steps = array('corredores' , 'something' , 'something'...and so one );

我试图根据第一步中保存在数组中的值显示表单,我正在做这样的事情

switch($steps[0]){
    case 'categorias' :
    include  'obj/categorias.php';
                //$step='categorias';
                break;
    case 'corredores':
    include 'obj/corredores.php';
                //$step='corredores';
                break;
    case 'monedas':
    include 'obj/monedas.php';
                //$step='monedas';
                break;
    case 'location':
    include 'obj/location.php';
                //$step='location';
                break;
    default:
    break;  
}

//在这里,我尝试匹配数组中的下一个值,该值将在每个步骤中保存在该发布值中

if(isset($_POST['next']))
{
include  'obj/'.$_POST["next"].'php';
                //$step='categorias';
                break;  

}
else {


}

因此,当我单击每个文件的下一个按钮时,它应与数组中的下一个值匹配并显示相关文件

每个文件的HTML内容具有以下结构

<form method="post" action="./index.php" name="form_name">
<table><tr><th>Some Name</td></tr>
<tr><td><input type="submit" value="next"></td></tr></table>

  <input type="hidden" name="next" value="<?php  //here i will add the value for next ?>">
</form>

谁能给我一些建议

据我了解,您需要使用POST var搜索数组,然后在input标签的value属性中回显下一个值。

<?php 
 $key=0;
 if(isset($_POST['next'])){
     include  'obj/'.$_POST["next"].'php';
     $key = array_search($_POST["next"], $steps);
     if($key !== false) $key++;
     if($key==count($steps)) $key=0; 
       //if post contains last element then set $key to first OR do whatever you want
     break;  
 }
 else { }
 ?>

<input type="hidden" name="next" value="<?php echo $steps[$key]; ?>">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM