Jump to content

javascript -serialize a js arrary into a php array


phpfreakjav

Recommended Posts

I just wanted to share this code because it took me a while to figure howto do this.

<html>
<head>
	<script  language="Javascript">
//alternative 1
			function php_serialize(obj)
							{
								var string = '';

								if (typeof(obj) == 'object') {
									if (obj instanceof Array) {
										string = 'a:';
										tmpstring = '';
										count = 0;
										for (var key in obj) {
											tmpstring += php_serialize(key);
											tmpstring += php_serialize(obj[key]);
											count++;
										}
										string += count + ':{';
										string += tmpstring;
										string += '}';
									} else if (obj instanceof Object) {
										classname = obj.toString();

										if (classname == '[object Object]') {
											classname = 'StdClass';
										}

										string = 'O:' + classname.length + ':"' + classname + '":';
										tmpstring = '';
										count = 0;
										for (var key in obj) {
											tmpstring += php_serialize(key);
											if (obj[key]) {
												tmpstring += php_serialize(obj[key]);
											} else {
												tmpstring += php_serialize('');
											}
											count++;
										}
										string += count + ':{' + tmpstring + '}';
									}
								} else {
									switch (typeof(obj)) {
										case 'number':
											if (obj - Math.floor(obj) != 0) {
												string += 'd:' + obj + ';';
											} else {
												string += 'i:' + obj + ';';
											}
											break;
										case 'string':
											string += 's:' + obj.length + ':"' + obj + '";';
											break;
										case 'boolean':
											if (obj) {
												string += 'b:1;';
											} else {
												string += 'b:0;';
											}
											break;
									}
								}

								return string;
			}

			function displayArray(x)
							{//only displays 1 dimensional array
								for (i=0;i<x.length;i++)
									{
										document.write(x[i] + "<br >");
									}
			}
//alternative 2
			function js_array_to_php_array (a)
			// This converts a javascript array to a string in PHP serialized format.
			// This is useful for passing arrays to PHP. On the PHP side you can
			// unserialize this string from a cookie or request variable. For example,
			// assuming you used javascript to set a cookie called "php_array"
			// to the value of a javascript array then you can restore the cookie
			// from PHP like this:
			//    <?php
			//    session_start();
			//    $my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
			//    print_r ($my_array);
			//    ?>
			// This automatically converts both keys and values to strings.
			// The return string is not URL escaped, so you must call the
			// Javascript "escape()" function before you pass this string to PHP.
			{
			    var a_php = "";
			    var total = 0;
			    for (var key in a)
			    {
			        ++ total;
			        a_php = a_php + "s:" +
			                String(key).length + ":\"" + String(key) + "\";s:" +
			                String(a[key]).length + ":\"" + String(a[key]) + "\";";
			    }
			    a_php = "a:" + total + ":{" + a_php + "}";
			    return a_php;
			}



			var x = new Array("1","2");
			var x1=new Array();
			x1=x;
			displayArray(x);
			displayArray(x1);
			document.write("Serializing starts <br/>");
			x=php_serialize(x);
			displayArray(x);

			document.write("Serializing starts <br/>");
			x1=js_array_to_php_array(x);
			displayArray(x1);
			escape();


	</script>

</head>

<body></body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.