创建所需的数组

时间:2014-10-21 22:16:41

标签: javascript php jquery

我通过ajax上传文件,我需要安排我的$_FILES数组来定制我的需求。

目前这段代码:

<input id='athletes_gid' type='file' name='athletes_gid[]' multiple value="1" />

<script type="text/javascript">
// Variable to store your files
var files;

// Add events
$('#athletes_gid').on('change', prepareUpload);

// Grab the files and set them to our variable
function prepareUpload(event)
{
    files = event.target.files;
}

var data = new FormData();
$.each(files, function(key, value)
{
    data.append(key, value);
});

像我这样构造我的数组:

array (size=2)
  0 => 
    array (size=5)
      'name' => string '1398423008894.jpg' (length=17)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\Program Files\wamp\tmp\php40BC.tmp' (length=37)
      'error' => int 0
      'size' => int 116496
  1 => 
    array (size=5)
      'name' => string '1239049963136.jpg' (length=17)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\Program Files\wamp\tmp\php40CD.tmp' (length=37)
      'error' => int 0
      'size' => int 42415

如何主要在.each循环中编写上面的代码,以便数组如下所示:

array (size=5)
  'name' => 
    array (size=2)
      0 => string '1398423008894.jpg' (length=17)
      1 => string '1239049963136.jpg' (length=17)
  'type' => 
    array (size=2)
      0 => string 'image/jpeg' (length=10)
      1 => string 'image/jpeg' (length=10)
  'tmp_name' => 
    array (size=2)
      0 => string 'C:\Program Files\wamp\tmp\php172F.tmp' (length=37)
      1 => string 'C:\Program Files\wamp\tmp\php1740.tmp' (length=37)
  'error' => 
    array (size=2)
      0 => int 0
      1 => int 0
  'size' => 
    array (size=2)
      0 => int 116496
      1 => int 42415

1 个答案:

答案 0 :(得分:0)

简单的密钥交换功能

function array_keys_flip($oldArray) {
    foreach ($oldArray as $k1 => $v1) {
      foreach ($v1 as $k2 => $v2) {
        $newArray[$k2][$k1] = $v2
      }
    }
    return $newArray;
}