note 109958 modified in reserved.variables.files by danbrown

From: Date: Thu, 06 Sep 2012 16:53:55 +0000
Subject: note 109958 modified in reserved.variables.files by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190542@lists.php.net to get a copy of this message
A nice trick to reorder the $_FILES array when you use a input name as array is:

<?php
function diverse_array($vector) {
 $result = array();
 foreach($vector as $key1 => $value1)
 foreach($value1 as $key2 => $value2)
 $result[$key2][$key1] = $value2;
 return $result;
}
?>

will transform this:

array(1) {
 ["upload"]=>array(2) {
 ["name"]=>array(2) {
 [0]=>string(9)"file0.txt"
 [1]=>string(9)"file1.txt"
 }
 ["type"]=>array(2) {
 [0]=>string(10)"text/plain"
 [1]=>string(10)"text/html"
 }
 }
}

into:

array(1) {
 ["upload"]=>array(2) {
 [0]=>array(2) {
 ["name"]=>string(9)"file0.txt"
 ["type"]=>string(10)"text/plain"
 },
 [1]=>array(2) {
 ["name"]=>string(9)"file1.txt"
 ["type"]=>string(10)"text/html"
 }
 }
}

just do:

<?php $upload = diverse_array($_FILES["upload"]); ?> --was-- A nice trick to reorder the $_FILES array when you use a input name as array is: function diverse_array($vector) { $result = array(); foreach($vector as $key1 => $value1) foreach($value1 as $key2 => $value2) $result[$key2][$key1] = $value2; return $result; } will transform this: array(1) { ["upload"]=>array(2) { ["name"]=>array(2) { [0]=>string(9)"file0.txt" [1]=>string(9)"file1.txt" } ["type"]=>array(2) { [0]=>string(10)"text/plain" [1]=>string(10)"text/html" } } } into: array(1) { ["upload"]=>array(2) { [0]=>array(2) { ["name"]=>string(9)"file0.txt" ["type"]=>string(10)"text/plain" }, [1]=>array(2) { ["name"]=>string(9)"file1.txt" ["type"]=>string(10)"text/html" } } } just do: $upload = diverse_array($_FILES["upload"]); http://php.net/manual/en/reserved.variables.files.php

« previous php.notes (#190542) next »