Php foreach循环只输出$\u POST中的最后一个数组值

Php foreach循环只输出$\u POST中的最后一个数组值,php,arrays,Php,Arrays,我想创建一个批量更新页面,所以当用户点击复选框,然后编辑文章来更新文章 下面是一些来自数据库的表单数据,我在这里以html的形式插入: <form method="post"> <table border="1"> <tr> <td>Select</td> <td>url pic</td> <td>url web</td

我想创建一个批量更新页面,所以当用户点击复选框,然后编辑文章来更新文章

下面是一些来自数据库的表单数据,我在这里以html的形式插入:

<form method="post"> 
    <table border="1">
      <tr>
         <td>Select</td>
         <td>url pic</td>
         <td>url web</td>
      </tr>
      <tr>
         <td><input type='checkbox' value='2'  name='pp[]'> 2  0  <td>
         <td><input type="text" value="lvTbHafU1L2gqnmuSVMrWZzkcGJxORFsjpg"  name="img[2]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[2]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='3'  name='pp[]'> 3  0 <td>
         <td><input type="text" value="Da0qf3yKRglNewH6X5n9zShLGubZVQtxjpg"  name="img[3]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[3]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='4'  name='pp[]'> 4  0 <td>
         <td><input type="text" value="SGdQJ8h5CjHPkEbYpF9oglatsTfyc0nA.jpg"  name="img[4]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[4]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='5'  name='pp[]'> 5  0 <td>
         <td><input type="text" value="36247u089pt51l.jpg"  name="img[5]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[5]"></td>
      </tr>
      <tr>
         <td><input type='checkbox' value='6'  name='pp[]'> 6  0  <td>
         <td><input type="text" value="7a5083ou41269s.jpg"  name="img[6]"></td>
         <td><input type="text" value="http://google.com"  name="siteurl[6]"></td>
      </tr>
    </table>
   <input type="submit" name="sb" />
</form>
这里是php代码:

<?php
     if (isset($_POST['sb'])) {
              $option = array("pp");
              $result = array();

              foreach ( $option as $key ) {
                  $result = $_POST[$key];   
              }

             $result2 = array();
             $options = array("img","siteurl");

             foreach ( $result as $keys => $value ) {
                foreach ( $options as $options_key ) {
                   $result2[$value] = array( $options_key => $_POST[$options_key][$value] ); // problem !
                }
             }
        }

   echo '<pre>';
   print_r ($result2);
   echo '</pre>';
?>
因为它只显示了中的最后一个值:

Array
(
    [5] => Array
        (
            [siteurl] => http://google.com
        )

    [6] => Array
        (
            [siteurl] => http://google.com
        )

)
输出为:

Array
(
    [5] => Array
        (
            [img] => 36247u089pt51l.jpg
            [siteurl] => http://google.com
        )

    [6] => Array
        (
            [img] => 7a5083ou41269s.jpg
            [siteurl] => http://google.com
        )

)
我需要这样的输出:

        $result2[$value] = array( 'img' => $_POST['img'][$value] ,
        'siteurl' => $_POST['siteurl'][$value]
         );
注意:如果我使用此代码:

[5] => Array
    (
        [img] => 36247u089pt51l.jpg
        [siteurl] => http://google.com
    )
输出将是:

$result2[$value] = array( $options_key => $_POST[$options_key][$value] ); // problem !
这是可以的,但我不想做这样的事情,因为我有太多的值(我在这段代码中只写了两个),我想知道如何解决这个问题

if( !isset($result2[$value])) $result2[$value] = array();
$result2[$value][$options_key] = $_POST[$options_key][$value];
改为:


你没有在foreach循环中输出任何内容我在foreach循环中使用
foreach
这部分没问题,我的问题是
$options=array(“img”,“siteurl”)仅显示“$result2[$value]=array($options\u key=>$\u POST[$options\u key][$value])中的最后一个值`非常感谢您,如果(!isset($result2[$value])
您能解释一下这个
吗?如果可能的话,我将非常感谢您向我解释为什么我的代码不起作用,因为我认为我的循环是正确的。您的代码会用一个只有一个键的新数组(最后一个键)重复覆盖
$result2[$value]
数组。如果数组不存在,我的代码将初始化它,然后向它添加一个键,而不是完全替换它。@Niet
if(!isset($result2[$value]))$result2[$value]=array()完全没有必要,对吗?这个答案缺少教育意义上的解释。
[5] => Array
    (
        [img] => 36247u089pt51l.jpg
        [siteurl] => http://google.com
    )
$result2[$value] = array( $options_key => $_POST[$options_key][$value] ); // problem !
if( !isset($result2[$value])) $result2[$value] = array();
$result2[$value][$options_key] = $_POST[$options_key][$value];