search

Home  >  Q&A  >  body text

How to create an array within an array?

I have these two arrays. I want to create an array within an array like this. How can I do this. The code is posted below. This is the image I'm sending in 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
                )

        )

)

P粉744831602P粉744831602265 days ago464

reply all(1)I'll reply

  • P粉211600174

    P粉2116001742024-04-02 20:07:42

    You can simply use array_map to achieve this, wrapping each element into another array:

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

    reply
    0
  • Cancelreply