簡體   English   中英

foreach循環只輸出$ _POST中的最后一個數組值

[英]foreach loop only outputs last array value in $_POST

我想創建一個批量更新頁面,所以當用戶點擊復選框然后編輯帖子來更新帖子。

這里有一些來自數據庫的表單數據,我在這里插入為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>

現在我想:

  1. Find checked checboxes - ok
  2. Get checked checkboxes values => i have a problem at this setp

這里的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>';
?>

我在這段代碼中遇到了問題

 $result2[$value] = array( $options_key => $_POST[$options_key][$value] ); 

因為它只顯示最后一個值:

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

輸出是:

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
    )

它會沒問題,但我不想做這樣的事情,因為我有太多的值(我在這段代碼中只寫了2個)並想知道如何解決這個問題。

$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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM