-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
                )

        )

)
5
  • 2
    Can I ask why you want it like that? What is the purpose? Commented May 13, 2022 at 9:18
  • I need to upload the image using curl. So i need to match this array. Commented May 13, 2022 at 9: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. Commented May 13, 2022 at 9: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. Commented May 13, 2022 at 9:25
  • Related: How to cast variable to array Commented May 14, 2022 at 11:57

3 Answers 3

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']
);
Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.