2

I am using dropzone to get the files uploaded to my Folder. Successfully getting the array of files.

   foreach($_FILES as $file) {
       print_r($file);
    }

Current Output:

Array
(
    [name] => Array
        (
            [0] => Image.PNG
            [1] => sadssadsa.PNG
        )

    [type] => Array
        (
            [0] => image/png
            [1] => image/png
        )

    [tmp_name] => Array
        (
            [0] => C:\Users\CH MANAN\AppData\Local\Temp\php48B6.tmp
            [1] => C:\Users\CH MANAN\AppData\Local\Temp\php48B7.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 291647
            [1] => 112790
        )
)

Expected output:

array
(
    [0] => array
    (
        [name] => Image.PNG
        [type] => image/png
        [tmp_name] => C:\Users\CH MANAN\AppData\Local\Temp\php48B6.tmp
        [error] => 0
        [size] => 291647
    )

    [1] => array
    (
        [name] => sadssadsa.PNG
        [type] => image/png
        [tmp_name] => C:\Users\CH MANAN\AppData\Local\Temp\php48B7.tmp
        [error] => 0
        [size] => 112790
    )
)

Tried various loops in the parent loop but not getting the expected results. Someone can help here.

Pankaj
  • 931
  • 8
  • 15
MR_AMDEV
  • 1,712
  • 2
  • 21
  • 38

4 Answers4

2

You can use this:

$keys = array_keys($_FILES); // get all the fields name
$res = array_map(null, ...array_values($_FILES)); // group the array by each file
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res); // insert the field name to result array

Documentation:

array-keys, array-map and array-combine

Live example: 3v4l

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • array_combine(): Both parameters should have an equal number of elements – MR_AMDEV Mar 10 '19 at 12:18
  • getting `array(5) { ["name"]=> bool(false) ["type"]=> bool(false) ["tmp_name"]=> bool(false) ["error"]=> bool(false) ["size"]=> bool(false) }` – MR_AMDEV Mar 10 '19 at 12:20
  • What output you get for `$res = array_map(null, ...$_FILES);`? (maybe I miss the $_FILES structure) – dWinder Mar 10 '19 at 12:20
  • `array(5) { ["name"]=> array(2) { [0]=> string(13) "sadssadsa.PNG" [1]=> string(8) "SLIP.PNG" } ["type"]=> array(2) { [0]=> string(9) "image/png" [1]=> string(9) "image/png" } ["tmp_name"]=> array(2) { [0]=> string(47) "C:\Users\CH MANAN\AppData\Local\Temp\php5BC.tmp" [1]=> string(47) "C:\Users\CH MANAN\AppData\Local\Temp\php5CC.tmp" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> array(2) { [0]=> int(112790) [1]=> int(112968) } } ` – MR_AMDEV Mar 10 '19 at 12:21
  • @MR_AMDEV Problem was with missing `array_values` - updated – dWinder Mar 10 '19 at 12:25
  • Works perfectly! – MR_AMDEV Mar 10 '19 at 12:37
  • Why `...` before array_values ? – MR_AMDEV Mar 10 '19 at 12:43
  • This php operator who break array of array to separate arrays - because array map takes array and not array of arrays – dWinder Mar 10 '19 at 12:44
1

Look at this example:

$source = [
    'name' => [
        'test1',
        'test2'
    ],
    'type' => [
        'jpg',
        'png'
    ]
];
$result = [];

foreach ($source as $key => $subArray) {
    foreach ($subArray as $index => $value) {
        if (!array_key_exists($index, $result)) {
            $result[$index] = [];
        }
        $result[$index][$key] = $value;
    }
}

var_dump($result);

it first looks at the (file) index. if the index does not exist in $result it will be added. And than it adds the key with value to the corresponding index.

you should work on the basics (manipulating multidimensional arrays et cetera) before advancing. do you have a tutorial or sth else you follow and learn from?

Sindhara
  • 1,423
  • 13
  • 20
1

You can loop the array and use array_combine and array_column to transform the array.

foreach($_FILES as $file) {
    $keys = array_keys($file);
    foreach($file['name'] as $key => $f){
        $new[] = array_combine($keys, array_column($file, $key));
    }
}
var_dump($new);

See working example:
https://3v4l.org/6lL8J

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • As you can see my expected output requires one array and Keys should have value not arrays. – MR_AMDEV Mar 10 '19 at 12:27
  • @MR_AMDEV **THEY ARE EXACTLY THE SAME!** Look at this then, now I use print_r. Does it look better? https://3v4l.org/cOlUk – Andreas Mar 10 '19 at 12:30
  • @MR_AMDEV and opposed to the accepted answer mine does not create notices and warnings. https://3v4l.org/qWONV – Andreas Mar 10 '19 at 12:32
  • Thanks for that .Just overlooked to the code and realized that its the output. – MR_AMDEV Mar 10 '19 at 12:35
0

The output for $_FILES that you see is a standard output for multiple files uploaded. You can iterate over one subarray of $_FILES (for example name) and get values from other subarrays under the same key:

foreach ($_FILES['name'] as $key => $value) {
    echo $value, $_FILES['error'][$key], $_FILES['tmp_name'][$key];   // etc
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64