2011-12-01 124 views
0

我有一个数组(通过上载多个文件。生产),用于返回该:通过多维阵列循环来创建新的阵列

Array 
(
[name] => Array 
    (
     [0] => lightshow.png 
     [1] => mia.guyana.jpg 
     [2] => skanking.jpg 
    ) 

[type] => Array 
    (
     [0] => image/png 
     [1] => image/jpeg 
     [2] => image/jpeg 
    ) 

[tmp_name] => Array 
    (
     [0] => /Applications/MAMP/tmp/php/phph7f4nD 
     [1] => /Applications/MAMP/tmp/php/phppS2YDH 
     [2] => /Applications/MAMP/tmp/php/phptebtKr 
    ) 

[error] => Array 
    (
     [0] => 0 
     [1] => 0 
     [2] => 0 
    ) 

[size] => Array 
    (
     [0] => 160325 
     [1] => 153524 
     [2] => 29054 
    ) 

) 

我需要每个键值对拉成一个单独的阵列foreach循环内处理,将返回这样的事情:

Array 
(
[name] => lightshow.png 
[type] => image/png 
[tmp_name] => /Applications/MAMP/tmp/php/phph7f4nD 
[error] => 0 
[size] => 160325 
) 

任何想法?

回答

2

谢谢你们,我终于回答了我的问题测试出jsaloen &肖恩的回答这都是收盘后,但不完全是我想要的。我的解决方案可能有点不合适,但它的工作原理!

if (isset($_FILES['files'])) { 

     $userfiles = $_FILES['files']; 

     $limit = count($userfiles['name']); 

     $i = 0; 

     while ($i < $limit) {   

       $userfile = array(
       'name' => $userfiles['name'][$i], 
       'type' => $userfiles['type'][$i], 
       'tmp_name' => $userfiles['tmp_name'][$i], 
       'error' => $userfiles['error'][$i], 
       'size' => $userfiles['size'][$i] 
      ); 

      $i++; 

      echo "<pre>"; 
      print_r($userfile); 
      echo "</pre>"; 

     } 

    } 

我还没有试过deceze的答案呢,我还不太明白呢!它利用我还没有学到的东西!

2
$result = array(); 
foreach($original_array as $key => $category) { 
    foreach($category as $index => $value) { 
     $result[$index][$key] = $value; 
    } 
} 
1

而重复,但它的工作原理:

$merged = array_map(function ($name, $type, $tmp_name, $error, $size) { 
    return compact('name', 'type', 'tmp_name', 'error', 'size'); 
}, $array['name'], $array['type'], $array['tmp_name'], $array['error'], $array['size']); 
0

我会去这样的事情。

//our end result array 
$NewArray = array(); 

//loop through the array 
foreach($Array as $Key => $inner_array) 
{ 
    $i = 0; 

    //setup temp array to populate 
    $TempArray = array(); 


    foreach($inner_array as $inner_key => $inner_value) 
    { 
     //loop through the inner value 
     $TempArray[$inner_key] = $inner_array[$i]; 
    } 

    $i++; 

    //setup the next key to be the temp array 
    $NewArray[] = $TempArray; 

} 

这将返回一个数组一样

Array(
     Array 
     (
      [name] => lightshow.png 
      [type] => image/png 
      [tmp_name] => /Applications/MAMP/tmp/php/phph7f4nD 
      [error] => 0 
      [size] => 160325 
     ) 
     Array 
     (
      [name] => lightshow.png 
      [type] => image/png 
      [tmp_name] => /Applications/MAMP/tmp/php/phph7f4nD 
      [error] => 0 
      [size] => 160325 
     ) 
    )