fantomel Posted April 5, 2009 Share Posted April 5, 2009 Hello i'm having some trouble with pulling data from arrays i've got a function for normalize the array $_FILES but when i'm having multiple upload fields i'm having trouble accesing second array please can someone help me ? i've tried in each mode i know .. but still can't figure it out why i can't . function normalize() { $files = array (); foreach ($_FILES as $name => $file) { if (is_array($file['name'])) { foreach ($file['name'] as $i => $value) { $files[$name][] = array ( 'name'=>$value, 'type'=>$file['type'][$i], 'tmp_name'=>$file['tmp_name'][$i], 'error'=>$file['error'][$i], 'size'=>$file['size'][$i], ); } } else { $files[$name] = $file; } } return $files; } if ( isset ($_POST['Submit'])) { $files = normalize(); foreach ($files['userfile'] as $i => $file) { echo "<pre>"; var_dump($file); echo "</pre>"; } } if i would like to get data like $file[1][name] it doesn't get the data from the second array:( or the third.. only from the first array. Link to comment https://forums.phpfreaks.com/topic/152686-having-trouble-with-foreach-and-multiple-arrays/ Share on other sites More sharing options...
charleshill Posted April 5, 2009 Share Posted April 5, 2009 Here's what I do to normalize the $_FILES array as $files... // we do this so that even file upload fields that are not arrays, are processed as arrays... it's easier if (!is_array($_FILES[$key]['tmp_name'])) $_FILES[$key] = array( 'tmp_name' => array($_FILES[$key]['tmp_name']), 'name' => array($_FILES[$key]['name']), 'size' => array($_FILES[$key]['size']), 'error' => array($_FILES[$key]['error']), 'type' => array($_FILES[$key]['type']), ); $files = array(); foreach ($_FILES[$key]['tmp_name'] as $n => $dummy) if (!empty($_FILES[$key]['tmp_name'][$n])) $files[$n] = array( 'tmp_name' => $_FILES[$key]['tmp_name'][$n], 'name' => $_FILES[$key]['name'][$n], 'size' => $_FILES[$key]['size'][$n], 'error' => $_FILES[$key]['error'][$n], 'type' => $_FILES[$key]['type'][$n], ); $key is just the name of the file upload field(s) in your form. Link to comment https://forums.phpfreaks.com/topic/152686-having-trouble-with-foreach-and-multiple-arrays/#findComment-801808 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.