如何将 PHP 表单中的数据添加到数组中?

发布于 2024-07-13 08:20:53 字数 404 浏览 4 评论 0原文

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

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', 
);

If I have a loop that requests my data from my form:

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."',";       
}

How do I add that into the sample array below?:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

葵雨 2024-07-20 08:20:53

更小:

$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);
    }
}

Even smaller:

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

If you aren't absolutely sure that $_POST['checkbx'] exists and is an array you should prolly do:

$files = array();
if (is_array(@$_POST['checkbx']))
{
    foreach($_POST['checkbx'] as $file)
    {
        $files[] = basename($file);
    }
}
玩心态 2024-07-20 08:20:53

请记住,您还需要在 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]
}

?>

Remember that you also need to name these checkboxes in HTML with "[]" after their names. e.g.:

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

You will then be able to access them thusly:

<?php

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

?>
安静 2024-07-20 08:20:53
$files[] =$filename;

或者

array_push($files, $filename);
$files[] =$filename;

OR

array_push($files, $filename);
江心雾 2024-07-20 08:20:53

您可以使用 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 填充数组即可。

You can use the array_push function :

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

Will give :

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

Simply fill the array using array_push for each file.

爱本泡沫多脆弱 2024-07-20 08:20:53

大概是这样的:

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  
}

Probably like this:

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  
}
江湖正好 2024-07-20 08:20:53

我不完全确定您想要添加到该数组中的内容,但这是使用 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;
}

I'm not entirely sure what you want to add to that array, but here is the general method of 'pushing' data into an array using php:

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

for example you could do:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文