【发布时间】:2015-03-30 03:08:09
【问题描述】:
这是我的代码:
HTML(多次上传):
<input type="file" name="attached_photo_1" id="attached_photo_1" />
<input type="file" name="attached_photo_2" id="attached_photo_2" />
<input type="file" name="attached_photo_3" id="attached_photo_3" />
<input type="file" name="attached_photo_4" id="attached_photo_4" />
<input type="file" name="attached_photo_5" id="attached_photo_5" />
PHP
$photo_array = array(
$_FILES['attached_photo_1'],
$_FILES['attached_photo_2'],
$_FILES['attached_photo_3'],
$_FILES['attached_photo_4'],
$_FILES['attached_photo_5']
);
示例
现在假设有人只上传了照片 1 和 2 的图像,而图像 3、4 和 5 的 $_FILES 错误代码为 4(表示没有上传文件)。
示例输出(使用<?php print_r($photo_array); ?>):
Array
(
[0] => Array
(
[name] => test.png
[type] => image/png
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/php0JwXJc
[error] => 0
[size] => 3469655
)
[1] => Array
(
[name] => test-2.jpg
[type] => image/jpeg
[tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpv7qFc6
[error] => 0
[size] => 1666451
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[3] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[4] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
主要问题:
如何删除子数组[error] => 4 的所有父数组元素?因此,在上面的示例中,元素 [2]、[3] 和 [4] 将从数组中删除或取消设置。
【问题讨论】:
-
使用
name="attached_photo[]"并在提交时在 PHP 中获取一个数组会更好。
标签: php arrays function if-statement multidimensional-array