【问题标题】:How do I add data from a PHP form into an array?如何将 PHP 表单中的数据添加到数组中?
【发布时间】:2010-10-07 23:38:29
【问题描述】:

如果我有一个循环从我的表单中请求我的数据:

for ($i=0;$i < count($_POST['checkbx']);$i++) {
    // calculate the file from the checkbx
    $filename = $_POST['checkbx'][$i];
    $clearfilename = substr($filename, strrpos ($filename, "/") + 1);
    echo "'".$filename."',";       
}

如何将它添加到下面的示例数组中?:

$files = array(
  'files.extension',
  'files.extension', 
);

【问题讨论】:

    标签: php arrays forms request


    【解决方案1】:

    甚至更小:

    $files = array();
    foreach($_POST['checkbx'] as $file)
    {
        $files[] = basename($file);
    }
    

    如果您不确定 $_POST['checkbx'] 是否存在并且是一个数组,您应该这样做:

    $files = array();
    if (is_array(@$_POST['checkbx']))
    {
        foreach($_POST['checkbx'] as $file)
        {
            $files[] = basename($file);
        }
    }
    

    【讨论】:

      【解决方案2】:

      请记住,您还需要在 HTML 中为这些复选框命名,并在其名称后加上“[]”。例如:

      <input type="checkbox" name="checkbx[]"  ...etc... >
      

      这样您就可以访问它们了:

      <?php
      
      // This will loop through all the checkbox values
      for ($i = 0; $i < count($_POST['checkbx']); $i++) {
         // Do something here with $_POST['checkbx'][$i]
      }
      
      ?>
      

      【讨论】:

        【解决方案3】:
        $files[] =$filename;
        

        array_push($files, $filename);
        

        【讨论】:

          【解决方案4】:

          你可以使用array_push函数:

          <?php
          $stack = array("orange", "banana");
          array_push($stack, "apple", "raspberry");
          print_r($stack);
          ?>
          

          会给:

          Array
          (
              [0] => orange
              [1] => banana
              [2] => apple
              [3] => raspberry
          )
          

          只需使用 array_push 为每个文件填充数组。

          【讨论】:

            【解决方案5】:

            大概是这样的:

            for ($i=0;$i < count($_POST['checkbx']);$i++) {
            // calculate the file from the checkbx
            $filename = $_POST['checkbx'][$i];
            $clearfilename = substr($filename, strrpos ($filename, "/") + 1);
            
            $files[] = $filename; // of $clearfilename if that's what you wanting the in the array  
            }
            

            【讨论】:

              【解决方案6】:

              我不完全确定要向该数组添加什么,但这里是使用 php 将数据“推送”到数组中的一般方法:

              <?php
              $array[] = $var;
              ?>
              

              例如你可以这样做:

              for ($i=0;$i < count($_POST['checkbx']);$i++)
              {
                 // calculate the file from the checkbx
                 $filename = $_POST['checkbx'][$i];
                 $clearfilename = substr($filename, strrpos ($filename, "/") + 1);
              
                 echo "'".$filename."',";       
                 $files[] = $filename;
              }
              

              【讨论】:

                猜你喜欢
                • 2021-09-30
                • 2021-01-16
                • 2012-01-17
                • 1970-01-01
                • 2023-03-28
                • 2022-01-25
                • 1970-01-01
                • 2016-07-22
                相关资源
                最近更新 更多