Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gviaud's avatar

POST, $request and Arrays

Assuming an arbitrarily indexed array (I will not know the index names in advance), when posting Arrays to Laravel, I'm getting the word "Array" instead of the actual Array.

// Posting out this multidimensional array with curl:
$myData = [
    'A' => [123,456,789],
    'B' => [987,654,321],
    'C' => [192,384,576]
    ];

$ch = curl_init('http://myserver.com/test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

echo $output;
// Then viewing in controller like this
dd($request->all());

Results in:

array:3 [
  "A" => "Array"
  "B" => "Array"
  "C" => "Array"
]

How can I access the arbitrarily named elements and sub-arrays within the request? (I'd really like them to survive the request as an array)

0 likes
3 replies
gviaud's avatar

I've decided to simply json_encode the data and send it out that way, although I'm still curious to know if there is a solution to this.

Much thanks

gviaud's avatar

Finding myself running into this same issue again, would love not to encode as JSON first. Anyone else trying the same?

gviaud's avatar
gviaud
OP
Best Answer
Level 1

Ok, think I have this figured out.

It looks like the problem is actually with php curl_setup CURLOPT_POSTFIELDS inability to handle multidimensional arrays.

My new code uses http_build_query to encode my request array prior to transmittal with curl:

$ch = curl_init('http://myserver.com/test');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_quert($myData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

Hope this helps someone.

Please or to participate in this conversation.