【问题标题】:Change values in array by forms values send通过表单值发送更改数组中的值
【发布时间】:2014-10-10 15:25:36
【问题描述】:

我有 2 个数组。
第一个数组包含文本中简单 DB 的表名。
第二个数组包含每个表的值。

当我启动表单时,从表单发送的字段值与表数组的值相同,从表单中获取值。

脚本:

  <?php        
    if($_POST[send]=="ok") {
        /// The structure it´s that and fixed , in the array_1 and 2 /// 


$tables="name,phone,alias"; 
       $values="Jhon,55543232,johny25";

       /// Explode values in each case ///
       $exp_tables=explode(",",$tables); 
       $exp_values=explode(",",$values);
       /// Array for get values for each field /// 
       $i=0;
       foreach($exp_tables as $exp_table) { ${$exp_table}[]="".$exp_values[$i].""; $i++; }
         /// Bucle for get the result if vars send by form equal to the other vars and change by new value send form the form ///
          foreach($exp_tables as $table) {
             foreach($_POST as $key=>$value) {   
                  if($table=="".$key."") 
                    { 
                      print "".$_POST[$table]."";        
                    } 
                  else { print "".${$table}{0}."";}

                }
             }
         }
     ?>

html 表单

<form action="" method="post">
<input type="text" name="name" value="" />
<input type="submit" name="submit2" value"send" />
<input type="hidden" name="send" value="ok" />
</form>

在表单中,我在第一个字段中有“名称”,在数组表中,我有一个字段也称为名称。

当我发送表格时,我必须得到这个:

Jhon(change value by the value from the form),55543232,johny25

问题是重复所有时间值并且没有得到结果。

我的问题是:如何解决这个问题,以便放置我从表单发送的值,并在其他值与数组表中的名称相同时进行更改,但效果不是很好。

【问题讨论】:

  • 你能告诉我你期望什么输出吗?您是说 $name 值应该替换为表单的值吗?
  • 是的没错,与数组中同名的表单通过表单值发送改变值

标签: php foreach flat-file


【解决方案1】:

首先,您的变量$tables$values 不是数组而是字符串。

数组看起来像

array(value, value, value)

array( key=&gt; value, key=&gt; value, key value)

在你的情况下,你可以使用

$tables=array('name','phone','alias'); 
$values=array('Jhon','55543232','johny25');
   //or, to make even simpler
$values=array('name'=>'Jhon','phone'=>'55543232','alias'=>'johny25');

然后,如果您只想输出名称并检查名称是否与数组中的名称不同,您只需这样做:

if($_POST['name']==$values['name']){
     print "".$_POST['name']."";        
     } 
 else{
     print "".$values['name']."";
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    相关资源
    最近更新 更多