Last active
August 29, 2015 14:03
-
-
Save zbee/794d74033a62df5aad99 to your computer and use it in GitHub Desktop.
Format jQuery's serialize() in PHP as actual array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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