Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unserialize PHP Array in Javascript

I have a table with a load of rows of serialized arrays that I plan to request and pass it to JavaScript.

The problem is - is it possible to unserialize with JavaScript rather than PHP ?

Otherwise I will have to load all the rows, loop them and unserialize them and assign them to a temporary PHP array and then json_encode it back to JavaScript which seems highly inefficient if I can send the data still serialized so that JavaScript can unserialize the data when it needs to.

Is there a built in Javascript function that does it or will I have to loop the rows in PHP before I encode it?

Note I am not using jQuery.

EDIT: Example of my serialized data in PHP from my table:

a:8:{i:0;a:2:{i:0;i:10;i:1;i:11;}i:1;a:2:{i:0;i:9;i:1;i:11;}i:2;a:2:
{i:0;i:8;i:1;i:11;}i:3;a:2:{i:0;i:8;i:1;i:10;}i:4;a:2:{i:0;i:8;i:1;i:9;}i:5;a:2:
{i:0;i:8;i:1;i:8;}i:6;a:2:{i:0;i:8;i:1;i:7;}i:7;a:2:{i:0;i:8;i:1;i:6;}}
like image 285
Sir Avatar asked Jan 09 '13 02:01

Sir


People also ask

How do you unserialize an array in PHP?

PHP unserialize() Function$data = serialize(array("Red", "Green", "Blue")); echo $data . "<br>"; $test = unserialize($data);

How do you unserialize data in Javascript?

unserialize() function The unserialize() converts serialized data to actual data. Example of php. js unserialize() function: In the following web document, unserialize() function converts a serialize data to actual format.

How to unserialize form data in PHP?

PHP: unserialize() function The unserialize() converts to actual data from serialized data. The serialized string. Return value: Converted value.

What is serialize and unserialize in PHP?

serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object.


1 Answers

Php.js has javascript implementations of unserialize and serialize:

http://phpjs.org/functions/unserialize/

http://phpjs.org/functions/serialize/

That said, it's probably more efficient to convert to JSON on the server side. JSON.parse is going to be a lot faster than PHP.js's unserialize.

like image 88
dieki Avatar answered Oct 16 '22 04:10

dieki