0

我使用 cakephp FormHelper 生成 html 表单代码。

echo $this->Form->create('newGallery.', array('type'=>'file'));
    echo $this->Form->input('Photos',array(
            'type'  =>  'file',
            'label' =>  'Photos (jpg,png,gif)',
            'name'  =>  'upload[]',
            'required',
            'multiple'
        ));
echo $this->Form->end('Create');

但结果是

Array
(
    [upload] => Array
        (
            [name] => Array
                (
                    [0] => Pic1.jpg
                    [1] => Pic2.jpg
                )
            [type] => Array
                (
                    [0] => image/jpeg
                    [1] => image/jpeg
                )
            [tmp_name] => Array
                (
                    [0] => C:\wamp\tmp\phpB073.tmp
                    [1] => C:\wamp\tmp\phpB084.tmp
                )
            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                )
            [size] => Array
                (
                    [0] => 216302
                    [1] => 107102
                )
        )
)

我需要对同一个文件的数据进行分组,以便我可以使用 foreach 循环来处理。这是我需要的结果

Array
(
    [upload] => Array
        (
            [0] => Array
                (
                    [name] => Pic1.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\wamp\tmp\phpB073.tmp
                    [error] => 0
                    [size] => 216302
                )
            [1] => Array
                (
                    [name] => Pic2.jpg
                    [type] => image/jpeg
                    [tmp_name] => C:\wamp\tmp\phpB084.tmp
                    [error] => 0
                    [size] => 107102
                )
        )
)

确实感谢您的帮助。

4

1 回答 1

0

我认为这可以帮助你:)

/* create new empty array */
$result=array();

/* create new index */
$i=0;

/* foreach in your $upload or other array */
foreach($upload['upload'] as $key => $val){
    foreach($val as $sub_val){
        $result[$i][$key]=$sub_val;
        $i++;
    }
    $i=0;
}
于 2012-10-25T15:54:54.023 回答