0

I have a form submitted with a single input of type of file name: photo[profile][]. I get the this array:

Array
(
[photo] => Array
    (
        [name] => Array
            (
                [profile] => Array
                    (
                        [0] => Chrysanthemum.jpg
                        [1] => Desert.jpg
                    )

            )

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

            )

        [tmp_name] => Array
            (
                [profile] => Array
                    (
                        [0] => C:\xampp\tmp\php9DCC.tmp
                        [1] => C:\xampp\tmp\php9E0B.tmp
                    )

            )

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

            )

        [size] => Array
            (
                [profile] => Array
                    (
                        [0] => 879394
                        [1] => 845941
                    )

            )

    )

)

So, i want a recursive function which will convert my array to this:

Array
(
    [photo] => Array
        (
            [profile] => Array
                (
                    [0] => Array
                    (
                        [name] => Chrysanthemum.jpg
                        [type] => image/jpeg
                        [tmp_name] => C:\xampp\tmp\php9DCC.tmp
                        [error] => 0
                        [size] => 845941
                    )
                   [1] => Array
                    (
                        [name] => Desert.jpg
                        [type] => image/jpeg
                        [tmp_name] => C:\xampp\tmp\php9DCC.tmp
                        [error] => 0
                        [size] => 845941
                    )
            )
    )
)

I need this because i want to upload more photos and set a config for each group of files.

4
  • Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect question And how to create a Minimal, Complete and Verifiable example SO is not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service You also have to show that you have made some effort to solve your own problem. Commented Oct 23, 2016 at 16:06
  • This is not rocket science. At least try to do it for yourself. Then if you fail you can ask here for help Commented Oct 23, 2016 at 16:07
  • I really tried to make this. I can't figure what so bad in this example. I know that could make a few loops and solve my problem, by this is not really smart and useful... Commented Oct 23, 2016 at 16:14
  • Nothing wrong with the example. The issue is you are asking US to write all your code for you. SO != Free coders Commented Oct 23, 2016 at 16:19

2 Answers 2

1

There is no native one, just use a loop.

$result = array();

foreach($_FILES as $key => $value){
   foreach($value as $key2 => $value2){
       foreach($value2 as $key3 => $value3){
           foreach($value3 as $index => $value4){
               $result[$key][$key3][$index][$key2] = $value4;
           }
       }         
   }         
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is good, but what if my array us deeper? Here is come the problem. That's why i need a recursive function. Anyway, thanks.
1

The solution using array_column function(available since PHP 5.5) and array_column function:

if (isset($_FILES['photo'])) {
    $quantity = count($_FILES['photo']['size']['profile']);        
    $photos = array_column($_FILES['photo'], 'profile');
    $ordered_list = ['photo' => ['profile' => []]];

    for ($i = 0; $i < $quantity; $i++) {
        $ordered_list['photo']['profile'][] = array_combine(['name', 'type', 'tmp_error', 'error', 'size'], array_column($photos, $i));
    }        
}

Now the $ordered_list contains the needed grouped data

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.