Skip to content

Instantly share code, notes, and snippets.

@zbee
Last active August 29, 2015 14:03
Show Gist options
  • Save zbee/794d74033a62df5aad99 to your computer and use it in GitHub Desktop.
Save zbee/794d74033a62df5aad99 to your computer and use it in GitHub Desktop.
Format jQuery's serialize() in PHP as actual array
Ok, so when you serialize a form in JQuery - $('#my-form').serialize() - you get something like
title=cake&date=2014-07-10 2:56PM&content=cake cake cake&tags=cake&new=post
Obviously, that isn't super useful to PHP and unserialize() does nothing for it.
So, I made unJQSerialize(); which is a little function to go through the serialized form and break it down into an actual PHP array.
When unJQSerialize() is used, you'll get something like
[title] => cake [date] => 2014-07-10 2:56PM [content] => cake cake cake [tags] => cake [new] => post
<?php
function unJQSerialize($data) {
$us = [];
$data = explode("&", $data);
for ($x = 0; $x < count($data); $x++) {
$usR = explode("=", $data[$x]);
$usT = $usR[0];
$usD = $usR[1];
$us[$usT] = $usD;
}
return $us;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment