我使用了一个易于为最终用户使用的多文件上传器,但是有一个问题是它总是添加一个额外的字段以允许用户从不同的文件夹中选择更多的文件。
现在的问题是它会产生这个
Array
(
[files] => Array
(
[name] => Array
(
[0] => 1450282558_circle-next-arrow-disclosure-glyph.png
[1] => 40525882_ml.jpg
[2] => pdf-sample.pdf
[3] =>
)
[type] => Array
(
[0] => image/png
[1] => image/jpeg
[2] => application/pdf
[3] =>
)
[tmp_name] => Array
(
[0] => /tmp/phpovbkoC
[1] => /tmp/phpNCISaS
[2] => /tmp/phpuRKS37
[3] =>
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 4
)
[size] => Array
(
[0] => 405
[1] => 218829
[2] => 7945
[3] => 0
)
)
)
每个部分的最后一个条目都是空的,有没有办法解决这个问题?我想删除/忽略让我们说[3]在名称,类型等等所以我避免那个空条目?
答案 0 :(得分:1)
您可以使用array_slice。如果您的数组存储在 $ data 中,则其工作方式如下:
foreach($data["files"] as $attr => $file) {
$data["files"][$attr] = array_slice($data["files"][$attr], 0, -1);
}
print_r ($data);
或者使用by-reference运算符(&
),可以缩短它:
foreach($data["files"] as &$file) {
$file = array_slice($file, 0, -1);
}
输出将是:
Array
(
[files] => Array
(
[name] => Array
(
[0] => 1450282558_circle-next-arrow-disclosure-glyph.png
[1] => 40525882_ml.jpg
[2] => pdf-sample.pdf
)
[type] => Array
(
[0] => image/png
[1] => image/jpeg
[2] => application/pdf
)
[tmp_name] => Array
(
[0] => /tmp/phpovbkoC
[1] => /tmp/phpNCISaS
[2] => /tmp/phpuRKS37
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 405
[1] => 218829
[2] => 7945
)
)
)
对array_slice的调用具有以下参数: