PHP $_POST converting array to string 'Array'

Posted on 22nd Jun 2012

I've been developing in PHP for about 5 years now, processing form data using $_POST on almost a daily basis. It was only yesterday though that I came across this little headache for the first time. What looks like and array, talks like an array, should be an array but is actually the 5 character string 'Array'!?

Here's the scenario I was in. I was trying to post some fairly normal form submission data (a few text inputs and a checkbox array) using cURL to an API that would in turn update a database record. I would then expect a JSON result from the server letting me know if all went well or if we had any errors in the form data or updating process. Seemed straight forward enough.

My basic cURL code was this.

<?php
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.co.uk/api/update/format/json'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST); $buffer = curl_exec($ch); curl_close($ch); $result = json_decode($buffer); ?>

I've used this code a few times before, and even in the same project. But there was something going on this time that was causing some of the $_POST data that should have been an array, to actually be converted into the string 'Array'. Weird huh? Using print_r() and var_dump() on $_POST was showing me the right data in the right format. But if I returned the $_POST data from the API method and used print_r() on the $result variable it would show any array now as a string.

More often than not Google will find me a result or something near enough to put me on the right track but after 3 hours searching I was drawing a blank! People had experienced similar issues previously but there never seemed to be a a solution by the end of the forum posts. Close to tears it eventually dawned on me that it must be something to do with the way cURL was handling multi-dimensional arrays. All my other forms in the project using the same code and that were working, weren't posting multi-dimensional arrays (an array in an array, for those not sure what on earth I'm talking about).

A little digging around cURL quite quickly informed me that in fact cURL does not handle these sorts of arrays well at all. The fact still remains however that I needed to send this data through, so the question was how to convert a multi-dimensional array into something like a string that could easily be sent but then on the receiving end convert the string back into the array?

Well fortunately the solution is not a complex one. PHP has 2 native functions that will do exactly what we need in this case, serialize() and unserialize().

We need to use the serialize() function to convert our form data in the global $_POST array into its string representation.

Simple enough, my new code is now.

<?php
$postArgs = array('data' => serialize($_POST)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://example.co.uk/api/update/format/json'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postArgs); $buffer = curl_exec($ch); curl_close($ch); $result = json_decode($buffer); ?>

A new step is now required on our receiving end in the API method now to convert our string back to it's original array. So in the receiving method we'll use the unserlialize() function like so.

<?php
function update_post() { // Convert the $_POST data from string representation back to multi-dimensional array $_POST = unserialize($_POST['data']); /* * Validate and Sanitize form data then update Database Record */ } ?>

So there we have it. The solution is a simple one but it was a real fight getting there. I hope it helps others who run into the same or similar issues.

I only got through by the power of Yorkshire Tea and stroopwafels.