0

I've seen lots of articles on how to serialise arrays from javascript to send to PHP files which the PHP file will then deserialise back into an array, etc...

I have a large-ish nested associative array in my javascript which I need to save to a string and at a later date retrieve and convert back into a string in javascript. No other program will ever touch this. It may be handy if the serialised data is human-readable but it is absolutely not a requirement.

There is an intermediate PHP file but it's only 4 lines long: all it does is save the string to a file and send it back when requested. I'm not interested in parsing the array in PHP.

What is the fastest and simplest way to convert a multi-nested associative array in javascript to a string and back again?

2
  • 1
    Have you looked into JSON? (Also, I assume by "associative array" you mean "object"?) Commented Jul 29, 2011 at 0:47
  • Well, I have constructed my array like: var data = new Array(); data["John"] = "John Smith"; etc. Commented Jul 29, 2011 at 1:05

4 Answers 4

4

JSON is a subset of the JavaScript language that is widely used as a serialization format, both for JavaScript and increasingly for other languages as well. It's human-readable, except that most implementations remove optional whitespace, which makes it a bit more difficult.

Modern browsers have JSON support out-of-the-box, for older ones you will need to include a library like json2.js.

To serialize, you use the JSON.stringify method.

var myObject = {a: 2, b: 3, c: [1, 2, 3]};
var serialized = JSON.stringify(myObject);
// serialized == "{"a":2,"b":3,"c":[1,2,3]}"

To unserialize, you use the JSON.parse method.

var recovered = JSON.parse(serialized);

Well, I have constructed my array like: var data = new Array(); data["John"] = "John Smith";

Ah, this is a problem. A JavaScript Array isn't meant to be used as an associative array, it's meant as a normal zero-indexed non-associative array. You can assign properties (key/value pairs) to any JavaScript object, which is why your code is working, but the fact thay your object is an Array is probably going to cause problems. If you just create a new Object() instead things should work better.

Sign up to request clarification or add additional context in comments.

3 Comments

JSON.stringify() returns [null,null,...,null,[]] however I'm absolutely positive that my array is populated.
Thanks. I'm in the process of re-writing my code (yuck) to use objects rather than associative arrays. I did a quick test and the JSON stringify works perfectly with objects.
It should work fine with arrays too as long as you only use "normal" numeric indexes (which includes array literals like ["something","something else","etc]).
0

You'll want to serialize your array into JSON:

serialized = JSON.stringify(myobject)

To get it back

myobject = JSON.parse(serialized)

http://www.json.org/js.html

Comments

0
var someArray = [];
someArray.push({someObjectProperty: 'someValue' });
someArray.push({someObjectProperty: 'someValue2' });

console.log(someArray);

var stringVersion = JSON.stringify(someArray);//You can save the string version anywhere...

console.log(stringVersion);

var someNewArray = JSON.parse(stringVersion);

console.log(someNewArray);

Comments

0

Everyone has explained this well on the Javascript side, but this is how it happens on the PHP side:

<?php
  $arr = Array(1, 2, 3, 'a', 'b', 'c');  // an array in PHP

  echo json_encode($arr); // output the array as JSON: [1,2,3,'a','b','c']
?>

<?php
  $json = "[1,2,3,'a','b','c']"; // a JSON string to be parsed by PHP

  $arr = json_decode($json); // this is our original array in PHP again
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.