【问题标题】:Arrays - Multiple Files Upload - Delete Parent Array Element Where Child Array ['error'] => 4数组 - 多个文件上传 - 删除父数组元素,其中子数组 ['error'] => 4
【发布时间】: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(表示没有上传文件)。

示例输出(使用&lt;?php print_r($photo_array); ?&gt;):

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] =&gt; 4 的所有父数组元素?因此,在上面的示例中,元素 [2]、[3] 和 [4] 将从数组中删除或取消设置。

【问题讨论】:

  • 使用name="attached_photo[]" 并在提交时在 PHP 中获取一个数组会更好。

标签: php arrays function if-statement multidimensional-array


【解决方案1】:

至少试一试。请参阅以下伪代码以了解如何实现这一点。

foreach($photo_array as $key => $values){

       if(isset($key['name']))
           $photo_array[$key] = $values;
       }
    }

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多