Here is a more general solution to seoguru's answer . It works with file input fields, the name of which contains any level of nested arrays, for example file , file[] (case for this question), file[english] , file[english][] , etc.
function rearrangeUploadArray(array $array) { if(!is_array(reset($array))) return $array; $rearranged = []; foreach($array as $property => $values) foreach($values as $key => $value) $rearranged[$key][$property] = $value; foreach($rearranged as &$value) $value = rearrangeUploadArray($value); return $rearranged; }
I understand that this answer is more complicated than it should be for this question, but maybe it can someday be useful to someone. An example of use is a localized upload form where several files can be downloaded for different languages. Then it would be advisable to have one file input field for each language, named, for example, file[english][] , file[german][] , file[spanish][] , etc. rearrangeUploadArray($_FILES['file']) will then return an array of the form
Array ( [english] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [1] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) [german] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) [spanish] => Array ( [0] => Array ( [name] => ... [type] => ... [tmp_name] => ... [error] => ... [size] => ... ) [...] ) )
source share