Unserialize PHP Array in Javascript
wrap json_encode
around unserialize
echo json_encode( unserialize( $array));
How to unserialize an array in Javascript
For javascript you probably don't want to use serialize
and unserialize
, but instead turn the result into JSON with json_encode
.
This format can be easily decoded by Javascript using JSON.parse()
.
Unserialize array from database with javascript
You can use this lib:
https://github.com/naholyr/js-php-unserialize
But in general, I can't understand why do you need this. Just convert in to the JSON object in PHP, then work without troubles with it in JS.
JS unserializing a PHP serialized array
You'll need to transform the array to JSON format, it will be easier for JS to understand, so do this in your html:
<input type="hidden" id="information" name="information" value="<?php echo htmlentities(serialize($hidden_information)); ?>" //This will comunicate with PHP
<input type="hidden" id="js_information" name="js_information" value="<?php echo json_encode($hidden_information); ?>" />
The information input will be use to comunicate with PHP and js_information for comunicating with JS. Then you can get that info in JS using:
var information = JSON.parse($('#js_information').val());
Note: You'll need to have PHP 5.2 or superior, also there's no need to put input in the jQuery selector because you have the ID specified
`
serialize to unserialize data
You can just iterate over the array and unserialize every single entry:
$unserialized = array();
foreach ($data as $serialized) {
$unserialized[] = unserialize($serialized);
}
PHP unserializing a JS serialized string of variables
You can use parse_str:
$values = array();
parse_str($_POST['userData'], $values);
echo $values['input1']; //Outputs 'meeting'
Serialize/Unserialize javascript and be read by PHP
To make JavaScript serialize in the syntax of PHP's serialize would require a custom JavaScript function, however you can do what you want with JSON.
To serialize to JSON in JavaScript you would use the stringify method of the JSON object:
JSON.stringify(myArray)
and to unserialize a JSON string in PHP you would use json_decode:
json_decode($myJsonArray)
If you want to support older browsers, you will have to include an external implementation of the JSON object. See Browser-native JSON support
How can I create a PHP array from this serialized string?
unserialize
is not for unserialising that format, it's the counterpart to serialize
, which produces a very different result. To parse a URL-encoded string in PHP, use parse_str
.
parse_str($_POST['serialized'], $result);
var_dump($result);
Of course, it's somewhat bizarre that you're sending a URL encoded string as $_POST['serialized']
… You should send it as the one and only request body, and PHP will automatically parse it into $_POST
and you won't have to do any of this.
Related Topics
Check Whether or Not a Cidr Subnet Contains an Ip Address
Tell Bots Apart from Human Visitors for Stats
PHP MySQL Search Multiple Tables Using a Keyword
Crop Whitespace from Image in PHP
Uploading File in PHP Server from Android Device
How to Compile on Linux to Share with All Distributions
How to Get Output of Proc_Open()
Package 'Php5-Gd' Has No Installation Candidate
Remove a String from the Beginning of a String
How to Dynamically Write a PHP Object Property Name
Using JSON_Encode on Objects in PHP (Regardless of Scope)