0

I have an array (of formdata) serialized in jQuery that I want to unserialize in PHP.

serialized data sample:

biztv_contentmanagementbundle_contenttype%5Btitle%5D=test&biztv_contentmanagementbundle_contenttype%5Bduration%5D%5Bhour%5D=0&biztv_contentmanagementbundle_contenttype%5Bduration%5D%5Bminute%5D=0&biztv_contentmanagementbundle_contenttype%5Bduration%5D%5Bsecond%5D=10&container=5&module=2&template=27&data_6=%3Cdiv+id%3D%22object_01%22+class%3D%22object_textbox%22+namn%3D%22object_01%22+style%3D%22z-index%3A+1%3B+font-family%3A+Arial%3B%22%3E%3Cdiv+class%3D%22object_inner%22+style%3D%22border%3A+1.1500000953674316px+solid+rgb(0%2C+0%2C+0)%3B+border-top-left-radius%3A+100%25%3B+border-top-right-radius%3A+100%25%3B+border-bottom-right-radius%3A+100%25%3B+border-bottom-left-radius%3A+100%25%3B+cursor%3A+move%3B+color%3A+rgb(0%2C+0%2C+0)%3B+background-color%3A+rgb(255%2C+255%2C+255)%3B%22%3Etest%3C%2Fdiv%3E%3C%2Fdiv%3E&data_0=transparent&data_1=&data_2=0&data_3=repeat&data_4=left&data_5=top&data_7=original&serialized=biztv_contentmanagementbundle_contenttype%255Btitle%255D%3Dtest%26biztv_contentmanagementbundle_contenttype%255Bduration%255D%255Bhour%255D%3D0%26biztv_contentmanagementbundle_contenttype%255Bduration%255D%255Bminute%255D%3D0%26biztv_contentmanagementbundle_contenttype%255Bduration%255D%255Bsecond%255D%3D10%26container%3D5%26module%3D2%26template%3D27%26data_6%3D%26data_0%3Dtransparent%26data_1%3D%26data_2%3D0%26data_3%3Drepeat%26data_4%3Dleft%26data_5%3Dtop%26data_7%3Doriginal%26serialized%3D%26biztv_contentmanagementbundle_contenttype%255B_token%255D%3D673914147a45cdf5d66d9dc97cdb0a2b61e6d3ae%26biztv_contentmanagementbundle_contenttype%255Bhour_start%255D%255Bhour%255D%3D%26biztv_contentmanagementbundle_contenttype%255Bhour_start%255D%255Bminute%255D%3D%26biztv_contentmanagementbundle_contenttype%255Bhour_end%255D%255Bhour%255D%3D%26biztv_contentmanagementbundle_contenttype%255Bhour_end%255D%255Bminute%255D%3D%26biztv_contentmanagementbundle_contenttype%255Bdate_start%255D%3D%26biztv_contentmanagementbundle_contenttype%255Bdate_end%255D%3D&biztv_contentmanagementbundle_contenttype%5B_token%5D=673914147a45cdf5d66d9dc97cdb0a2b61e6d3ae&biztv_contentmanagementbundle_contenttype%5Bhour_start%5D%5Bhour%5D=&biztv_contentmanagementbundle_contenttype%5Bhour_start%5D%5Bminute%5D=&biztv_contentmanagementbundle_contenttype%5Bhour_end%5D%5Bhour%5D=&biztv_contentmanagementbundle_contenttype%5Bhour_end%5D%5Bminute%5D=&biztv_contentmanagementbundle_contenttype%5Bdate_start%5D=&biztv_contentmanagementbundle_contenttype%5Bdate_end%5D=

Currently I am doing it like this:

parse_str($serialized,$dataArray);
    foreach($dataArray as $i) {
        var_dump($i);
        echo "<br /><br />";
    }

The problem is that I loose all labels, the resulting array only contains the values. In my case I want to loop over the data and do different stuff with each value depending on the label name (and this is kind of an API so I wouldn't always know what labels to expect from my form anyways, so I need to loop through them).

On this similar question (How do I PHP-unserialize a jQuery-serialized form?) I found this solution, is this the only/best way of accomplishing what I need?

function unserializeForm($str) {
    $returndata = array();
    $strArray = explode("&", $str);
    $i = 0;
    foreach ($strArray as $item) {
        $array = explode("=", $item);
        $returndata[$array[0]] = $array[1];
    }

    return $returndata;
}
Community
  • 1
  • 1
Matt Welander
  • 8,234
  • 24
  • 88
  • 138

1 Answers1

2

Here is what I ended up using, a modified version of that stolen unserializeForm that I found here How do I PHP-unserialize a jQuery-serialized form? cred to https://stackoverflow.com/users/1061944/murtaza-hussain

private function unserializeForm($str) {
    $returndata = array();
    $strArray = explode("&", $str);
    $i = 0;
    foreach ($strArray as $item) {
        $item = explode("=", $item);
        $returndata[$i][0] = $item[0];
        $returndata[$i][1] = $item[1];
        $i++;
    }

    return $returndata;
}
Community
  • 1
  • 1
Matt Welander
  • 8,234
  • 24
  • 88
  • 138