'How to make a array inside array?

I have this two array. I want to make a array inside an array like this. How can I achieve that. Code posted below. This is the image I am sending inside fileToUpload. The array I have

Array
(
    [fileToUpload] => Array
        (
            [name] => KERINOX COFFEE.jpg
            [type] => image/jpeg
            [tmp_name] => /opt/lampp/temp/phpuk5Uyo
            [error] => 0
            [size] => 2440617
        )

)

The array I want

Array
(
    [fileToUpload] => Array
        (
            [name] => Array
                (
                    [0] => KERINOX COFFEE.jpg
                )

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

            [tmp_name] => Array
                (
                    [0] => /opt/lampp/temp/php0LlvE2
                )

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

            [size] => Array
                (
                    [0] => 2502103
                )

        )

)


Solution 1:[1]

You can simply use array_map for this, to wrap each element into another array:

$data['fileToUpload'] = array_map(
  function($item) {
    return [$item];
  },
  $data['fileToUpload']
);

Solution 2:[2]

Easy enough to build a new array:

$array = [
    'fileToUpload' => [
        'name' => 'KERINOX COFFEE.jpg',
        'type' => 'image/jpeg',
        'tmp_name' => '/opt/lampp/temp/phpuk5Uyo',
        'error' => 0,
        'size' => 2440617
    ]
];
$newArray = ['fileToUpload' => []];

foreach ($array['fileToUpload'] as $key => $value)
    $newArray['fileToUpload'][$key][] = $value;

Solution 3:[3]

 $myArray = [];
 $myArray['fileToUpload']['name'] = array("file name");
 $myArray['fileToUpload']['type'] = array("file type");
 $myArray['fileToUpload']['tmp_name'] = array("temporary name");
 $myArray['fileToUpload']['error'] = array("error");
 $myArray['fileToUpload']['size'] = array("file size");

 echo "<pre>";
 print_r($myArray);

Output

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CBroe
Solution 2 Jacob Mulquin
Solution 3 Syed Hassan Zamir