【问题标题】:How convert the $_FILES array to the cleaner array?如何将 $_FILES 数组转换为更清洁的数组?
【发布时间】:2019-08-28 06:58:25
【问题描述】:

我在表单中使用以下循环来保存数据:

<form action="#" method="post" enctype="multipart/form-data">
    <?php foreach ($sites as $site) { ?>
        <div>
            <div>
                <input type="text" name="entry[<?= $site['id'] ?>][date]" id="repDate<?= $site['id'] ?>">
            </div>
            <div>
                <input type="file" name="entry[<?= $site['id'] ?>][file]" id="repFile<?= $site['id'] ?>">
            </div>
        </div>
    <?php } ?>
</form>

以上代码的输出如下:

array(1) {
  ["entry"]=>
  array(5) {
    ["name"]=>
    array(2) {
      [37]=>
      array(1) {
        ["file"]=>
        string(30) "my-file-test.docx"
      }
      [38]=>
      array(1) {
        ["file"]=>
        string(56) "resources_views.php"
      }
    }
    ["type"]=>
    array(2) {
      [37]=>
      array(1) {
        ["file"]=>
        string(71) "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
      }
      [38]=>
      array(1) {
        ["file"]=>
        string(17) "application/x-php"
      }
    }
    ["tmp_name"]=>
    array(2) {
      [37]=>
      array(1) {
        ["file"]=>
        string(14) "/tmp/phpahucFZ"
      }
      [38]=>
      array(1) {
        ["file"]=>
        string(14) "/tmp/phptnDGQR"
      }
    }
    ["error"]=>
    array(2) {
      [37]=>
      array(1) {
        ["file"]=>
        int(0)
      }
      [38]=>
      array(1) {
        ["file"]=>
        int(0)
      }
    }
    ["size"]=>
    array(2) {
      [37]=>
      array(1) {
        ["file"]=>
        int(12023)
      }
      [38]=>
      array(1) {
        ["file"]=>
        int(18174)
      }
    }
  }
}

这个数组就是上面代码的输出。

谁能告诉我如何以一种理智的方式迭代这个数组?

我想要以下相同的输出:

Array
(
    [0] => Array
        (
            [name] => foo.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpYzdqkD
            [error] => 0
            [size] => 123
        )

    [1] => Array
        (
            [name] => bar.txt
            [type] => text/plain
            [tmp_name] => /tmp/phpeEwEWG
            [error] => 0
            [size] => 456
        )
)

我使用下面的链接,但对我不起作用:

How can I iterate PHP $_FILES array?

Uploading multiple files

感谢您的帮助。

【问题讨论】:

  • 我没有得到你!你想要什么?
  • @Rahul,问题已更新。

标签: php forms file


【解决方案1】:

这里是简单的sn-p,

$result = [];
foreach ($_FILES['entry'] as $key => $value) {
    foreach ($value as $key1 => $value1) {
        $result[$key1][$key] = $value1['file'];    // fetching $key1 index and its file
    }
}
// if you want to reset indexes,
// $result = array_values($result);

Demo

输出

Array
(
    [37] => Array
        (
            [name] => my-file-test.docx
            [type] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
            [tmp_name] => /tmp/phpahucFZ
            [error] => 0
            [size] => 12023
        )

    [38] => Array
        (
            [name] => resources_views.php
            [type] => application/x-php
            [tmp_name] => /tmp/phptnDGQR
            [error] => 0
            [size] => 18174
        )

)

【讨论】:

  • @MostafaNorzade 乐于助人。如果此答案(考虑到我的答案的动态性质)或任何其他答案解决了您的问题,请将其标记为已接受。”
【解决方案2】:

是的,php 的 $_FILES 是疯狂的员工 :)

您可以通过例如name 的索引进行迭代:

$entries = $_FILES['entry'];

$result = [];
foreach (array_keys($entries['name']) as $index) {
    $row = [];
    // Use all possible keys
    foreach (['name', 'type', 'tmp_name', 'error', 'size'] as $key) {
        $row[$key] = $entries[$key][$index]['file'];
    }

    $result[$index] = $row;
}

var_dump($result);

【讨论】:

    【解决方案3】:

    您可以更改输入的names,使数组变得更清晰:

    name="entry[<?= $site['id'] ?>][date]"
    in:
    name="date[<?= $site['id'] ?>]"
    
    and 
    name="entry[<?= $site['id'] ?>][file]" 
    in:
    name="file[<?= $site['id'] ?>]" 
    

    $_FILES 数组应如下所示:

    [file]=>
      [name]     => [ 37=>... , 38=> ... ],
      [type]     => [ 37=>... , 38=> ... ],
      [tmp_name] => [ 37=>... , 38=> ... ],
      [error]    => [ 37=>... , 38=> ... ],
      [size]     => [ 37=>... , 38=> ... ],
    

    然后它只是循环:

    foreach($_FILES['file'] as $key=>$array){
    foreach($array as $nr=>$value){
      $my_files[$nr][$key] = $value;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2020-08-06
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多