【问题标题】:PHP array_replace only checked elements from $_POSTPHP array_replace 仅检查 $_POST 中的元素
【发布时间】:2015-09-21 12:19:18
【问题描述】:

如何替换从帖子提交的仅数组元素

我有一个带有多个复选框的表单,我将所有内容提交到一个 PHP 页面,我想在其中替换另一个数组。

表格:

$oldArray = array(
  "colors" => array(1 => "mandarin", "strawberry", "blueberry", "kiwi", "banana"),
  "other"  => array(1 => "test1", "test2", "test3", "test4")
);

目标页面:

$newArray = array(
  "colors" => array(1 => "orange", "red", "blue", "green", "yellow"),
  "other"  => array(1 => "example1", "example2", "example3", "example4")
);

print_r( array_replace($_POST['colors'], $newArray['colors']) );

如果我只检查两个元素,“mandarin”和“kiwi”,array_replace 返回:

Array (
  [1] => orange <--- OK
  [2] => strawberry 
  [3] => blueberry 
  [4] => green  <--- OK 
  [5] => banana
)

我怎样才能echo 只有$_POST 元素?

例如

 Array (
  [1] => orange
  [4] => green 
)

【问题讨论】:

  • array_replace 关闭数组键。如果没有看到您的表单,您可能会收到 $_POST['colors'][0]['orange']$_POST['colors'][1]['green'],这意味着您的密钥不匹配。
  • @MarcB orangegreen 是值,而不是键。至少根据上面发布的信息。
  • 是的,但除非 OP 将表单设置为具有 colors[5] 或其他任何字段名称,否则 $_POST 中的数组键不会对应于他的数组中的任何键。
  • @MarcB 我的表单结构是:&lt;form action="example.php" method="post" enctype="multipart/form-data"&gt; &lt;?php $oldArray = array( "colors" =&gt; array(1 =&gt; "mandarin", "strawberry", "blueberry", "kiwi", "banana"), "other" =&gt; array(1 =&gt; "test1", "test2", "test3", "test4") ); ?&gt; &lt;?php foreach($oldArray['colors'] as $color): ?&gt; &lt;input type="checkbox" name="colors[]" value="&lt;?php echo $color ?&gt;" &gt; &lt;?php echo $color ?&gt; &lt;?php endforeach; ?&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt;
  • 与其在您的问题中编辑解决方案,不如将其作为答案发布,如果它为当前问题添加了任何新内容。考虑接受最适合您的答案之一。

标签: php arrays forms post form-submit


【解决方案1】:

您应该通过发布数组键。试试这个:

$oldArray = array(
    "colors" => array(1 => "mandarin", "strawberry", "blueberry", "kiwi", "banana"),
    "other"  => array(1 => "test1", "test2", "test3", "test4")
);

echo '<select name="colors">';
foreach ($oldArray['colors'] as $key => $val)
{
    echo '<option value="'.$key.'">'.$val.'</option>';
}
echo '</select>';

然后在您收到的 PHP 页面中,您可以执行以下操作:

$newArray = array(
    "colors" => array(1 => "orange", "red", "blue", "green", "yellow"),
    "other"  => array(1 => "example1", "example2", "example3", "example4")
);

foreach($_POST['colors'] as $key)
{
    echo $newArray['colors'][$key];
}

【讨论】:

  • Ehi Styphon!谢谢你的建议。我的表单如下所示:&lt;form action="example.php" method="post" enctype="multipart/form-data"&gt; &lt;?php $oldArray = array( "colors" =&gt; array(1 =&gt; "mandarin", "strawberry", "blueberry", "kiwi", "banana"), "other" =&gt; array(1 =&gt; "test1", "test2", "test3", "test4") ); ?&gt; &lt;?php foreach($oldArray['colors'] as $color): ?&gt; &lt;input type="checkbox" name="colors[]" value="&lt;?php echo $color ?&gt;" &gt; &lt;?php echo $color ?&gt; &lt;?php endforeach; ?&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt;
  • @simo,有什么问题吗?使用键作为复选框值。
  • 如何通过post传递关键位置?现在前两个选中的元素替换了combine_arr 的前两个位置。
  • 说真的?!将键放在复选框的值中而不是名称中,就像我的示例对上面的选择框所做的那样!!!如果您自己无法弄清楚,那么您就不应该编程。这是非常基本的。
  • 喂!我按照你的建议做到了!在最后一个 foreach 中,正确的回显是 echo $newArray['colors'][$key]; 非常感谢!
【解决方案2】:

尝试:

 print_r(array_intersect ($_POST['colors'],$newArray['colors']));

【讨论】:

  • 感谢您的重播,这次只返回一个空的Array ( )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2011-08-01
相关资源
最近更新 更多