note 118294 added to reserved.variables.files

From: Date: Tue, 10 Nov 2015 12:05:57 +0000
Subject: note 118294 added to reserved.variables.files
Groups: php.notes 
Request: Send a blank email to php-notes+get-204131@lists.php.net to get a copy of this message
I've written this function to restructure deeply nested $_FILES arrays, so that the parameters for each file are grouped together. function restructure_files(array $input) { $output = []; foreach ($input as $name => $array) { foreach ($array as $field => $value) { $pointer = &$output[$name]; if (!is_array($value)) { $pointer[$field] = $value; continue; } $stack = [&$pointer]; $iterator = new \RecursiveIteratorIterator( new \RecursiveArrayIterator($value), \RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $key => $value) { array_splice($stack, $iterator->getDepth() + 1); $pointer = &$stack[count($stack) - 1]; $pointer = &$pointer[$key]; $stack[] = &$pointer; if (!$iterator->hasChildren()) { $pointer[$field] = $value; } } } } return $output; } Turns this: array (size=2) 'one' => array (size=5) 'name' => array (size=1) 'inner' => array (size=2) 11 => string 'DM4C2738.jpg' (length=12) 5 => string 'DM4C2760.jpg' (length=12) 'type' => array (size=1) 'inner' => array (size=2) 11 => string 'image/jpeg' (length=10) 5 => string 'image/jpeg' (length=10) 'tmp_name' => array (size=1) 'inner' => array (size=2) 11 => string '/private/var/tmp/phploOZRb' (length=26) 5 => string '/private/var/tmp/phpsFkmIh' (length=26) 'error' => array (size=1) 'inner' => array (size=2) 11 => int 0 5 => int 0 'size' => array (size=1) 'inner' => array (size=2) 11 => int 1031601 5 => int 674697 'two' => array (size=5) 'name' => string '9ncYySC.jpg' (length=11) 'type' => string 'image/jpeg' (length=10) 'tmp_name' => string '/private/var/tmp/phpuG99X9' (length=26) 'error' => int 0 'size' => int 882422 Into this: array (size=2) 'one' => array (size=1) 'inner' => array (size=2) 11 => array (size=5) 'name' => string 'DM4C2738.jpg' (length=12) 'type' => string 'image/jpeg' (length=10) 'tmp_name' => string '/private/var/tmp/phploOZRb' (length=26) 'error' => int 0 'size' => int 1031601 5 => array (size=5) 'name' => string 'DM4C2760.jpg' (length=12) 'type' => string 'image/jpeg' (length=10) 'tmp_name' => string '/private/var/tmp/phpsFkmIh' (length=26) 'error' => int 0 'size' => int 674697 'two' => array (size=5) 'name' => string '9ncYySC.jpg' (length=11) 'type' => string 'image/jpeg' (length=10) 'tmp_name' => string '/private/var/tmp/phpuG99X9' (length=26) 'error' => int 0 'size' => int 882422 ---- Server IP: 72.52.91.14 Probable Submitter: 92.27.124.179 ---- Manual Page -- http://php.net/manual/en/reserved.variables.files.php Edit -- https://master.php.net/note/edit/118294 Del: integrated -- https://master.php.net/note/delete/118294/integrated Del: useless -- https://master.php.net/note/delete/118294/useless Del: bad code -- https://master.php.net/note/delete/118294/bad+code Del: spam -- https://master.php.net/note/delete/118294/spam Del: non-english -- https://master.php.net/note/delete/118294/non-english Del: in docs -- https://master.php.net/note/delete/118294/in+docs Del: other reasons-- https://master.php.net/note/delete/118294 Reject -- https://master.php.net/note/reject/118294 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204131) next »