my little brain cant think of anything more efficient than this. a lot of loops and recursion going on here…
function walk($arr, $key) {
$ret = array();
foreach ($arr as $k => $v) {
if (is_array($v)) {
$ret[$k] = walk($v, $key);
} else {
$ret[$k][$key] = $v;
}
}
return $ret;
}
$arr = array();
foreach ($_FILES as $name => $values) {
// init for array_merge
if (!isset($arr[$name])) {
$arr[$name] = array();
}
if (!is_array($values['error'])) {
// normal syntax
$arr[$name] = $values;
} else {
// html array feature
foreach ($values as $attribute_key => $attribute_values) {
$arr[$name] = array_merge_recursive($arr[$name], walk($attribute_values, $attribute_key));
}
}
}
print_r($arr);
it supports both non html array syntax, as well as html array syntax even with multidim arrays, and will standardize them into a common(but repetative) format. just like you asked for. it doesnt immediately allow for you to create the objects. another walk will need to be performed. i cant think of any way around that short of using eval().
the resulting array could be walked again with yet another recursive function to instantiate your objects.
function make_objects($arr) {
$ret = array();
foreach ($arr as $k => $v) {
if (is_array($v)) {
$ret[$k] = make_objects($v);
} else {
return new UploadFile($arr['size'], $arr['name'], 'etc...');
}
}
return $ret;
}
print_r(make_objects($arr));
whew.
id love to see a better way to do this while supporting arbitrary nesting depth. maybe im approaching it wrong.