-1

I want to know the way of converting two dimensional array structure to the array of arrays. See the more specific example below:

I have data structure like this:

$_FILES["file"]["name"] = "aaaa.png";
$_FILES["file"]["type"] = "image/png"; 
$_FILES["file"]["tmp_name"] = "/tmp/phpQklCB7"; 
$_FILES["file"]["error"] = "0"; 
$_FILES["file"]["size"] = "37507";  

And I need it to be restructured to this data structure:

Array ( 
    [name] => Array ( [0] => aaaa.png ) 
    [type] => Array ( [0] => image/png ) 
    [tmp_name] => Array ( [0] => /tmp/phpIERHxT ) 
    [error] => Array ( [0] => 0 ) 
    [size] => Array ( [0] => 37507 ) 
)
Farside
  • 9,923
  • 4
  • 47
  • 60
  • 1
    Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – secelite Nov 16 '16 at 09:15
  • Possible duplicate of [Reindex array by increasing and decreasing all of top indexes](http://stackoverflow.com/questions/18393644/reindex-array-by-increasing-and-decreasing-all-of-top-indexes) – Xorifelse Nov 17 '16 at 02:18
  • updated subject, tags, and description to use more professional language – Farside Nov 18 '16 at 08:57

3 Answers3

0

Here you go,

<?php
$_FILES["file"]["name"]     = "aaaa.png";
$_FILES["file"]["type"]     = "image/png"; 
$_FILES["file"]["tmp_name"] = "/tmp/phpQklCB7"; 
$_FILES["file"]["error"]    = "0"; 
$_FILES["file"]["size"]     = "37507";  

foreach( $_FILES[ 'file' ] as $key => $value )
{
  $new_array[ $key ] = array( $value );
}

var_dump( $new_array );

Output

array(5) {
  ["name"]=>
  array(1) {
    [0]=>
    string(8) "aaaa.png"
  }
  ["type"]=>
  array(1) {
    [0]=>
    string(9) "image/png"
  }
  ["tmp_name"]=>
  array(1) {
    [0]=>
    string(14) "/tmp/phpQklCB7"
  }
  ["error"]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
  ["size"]=>
  array(1) {
    [0]=>
    string(5) "37507"
  }
}
Blinkydamo
  • 1,582
  • 9
  • 20
  • Why output not same as `Array ( [name] => Array ( [0] => aaaa.png ) [type] => Array ( [0] => image/png ) [tmp_name] => Array ( [0] => /tmp/phpIERHxT ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 37507 ) )` ? – robert dewo Nov 16 '16 at 08:27
  • The output is the same as you requesting in your question. – Blinkydamo Nov 16 '16 at 08:28
  • this is my array `[name] => Array ( [0] => aaaa.png )` and this is your `array ["name"]=> array(1) { [0]=> string(8) "aaaa.png" }` it's not same. – robert dewo Nov 16 '16 at 08:30
  • 1
    I'm sorry you have lost me, they are the same thing. Are you trying to remove the speech marks? this is the code working as you wanted - https://eval.in/678573 – Blinkydamo Nov 16 '16 at 08:32
0

Please try the below code

foreach($_FILES[ 'file' ] as $k=> $v )
{
   $arr[ $k ] = array( $v );
}
Ashish Kumar
  • 152
  • 7
-1

You can use this one:

$arr = array(
   'name' => array(
       0 => 'aaa.png'
   ),
   'type' => array(
       0 => 'image/png'
   ),
   'tmp_name' => array(
       0 => '/tmp/phpIERHxT'
   ),
   'error' => array(
       0 => 0
   ),
   'size' => array(
       0 => 37507
   )
);
vural
  • 381
  • 2
  • 11