-3

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
                )

        )

)
Sarkar
  • 61
  • 2
  • 12
  • 2
    Can I ask why you want it like that? What is the purpose? – ADyson May 13 '22 at 09:18
  • I need to upload the image using curl. So i need to match this array. – Sarkar May 13 '22 at 09:19
  • 1
    Why though? Does the server you're sending it to accept a different data format? Are you intending to just upload the array, or the actual file as well. What you're asking for seems a bit strange so i just wanted to clarify that it's definitely what you actually need, and not based on some prior misunderstanding. – ADyson May 13 '22 at 09:21
  • When I am trying to upload image using form the second array is generating. But when I trying to upload it through cURL the first array is generating and the image is not inserted in the server. But through the form it is inserting into the server. – Sarkar May 13 '22 at 09:25
  • Related: [How to cast variable to array](https://stackoverflow.com/q/5970270/2943403) – mickmackusa May 14 '22 at 11:57

3 Answers3

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']
);
CBroe
  • 91,630
  • 14
  • 92
  • 150
0

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;
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
0
 $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