-2

I have the following code which is generated in PHP:

<div id='container1' data-history="[["test", "test title", "", []]]">

I wish to have this accessible in javascript but when I use console.log(history[0]) it outputs '[' so its treating the array as a string.

Is there a way to have it so that javascript can read it as an array?

My PHP code is as follows (i removed the slashes and '' around the array)

echo '<div id="container1" data-history=[[0]["test", "test title", "", []]] data-current-url="'.$url.'">';
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Nuvolari
  • 1,103
  • 5
  • 13
  • 29

4 Answers4

1

If you removed the \ characters from it and added a , after the first array element, then it would be valid JSON and you could pass it through JSON.parse().

  <div id='container1' data-history='[[0],["test", "test title", "", []]]'></div>
  <script>
    var history = JSON.parse(document.getElementById('container1').getAttribute('data-history'));
    console.log(history);
    alert(history[1][1]);
  </script>

From a comment on another answer:

Its meant to be a multi dimensional array where index 0 contains '["test", "test title", "", []]'

You don't specify the property names in JavaScript or JSON array literal syntax. The position determines the property name.

data-history='[["test", "test title", "", []]]'>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Ok I did that and it gives me this error: "SyntaxError: JSON.parse: unexpected end of data". My new code: data-history=[[0]["test", "test title", "", []]] – Nuvolari Apr 18 '14 at 07:31
  • You've removed the `'` characters from around the attribute value. Don't do that. Attribute values containing spaces must be quoted. – Quentin Apr 18 '14 at 07:32
  • hmm, you are also missing a comma after the first array element – Quentin Apr 18 '14 at 07:36
0

Besides the escape characters (that are to be removed), the following string is not regular JSON:

[[0]["test", "test title", "", []]]

So eventually it won't be parsed by JSON.parse(). Maybe you ment following form:

[[0, "test", "test title", "", []]]

That is correctly parsed by JSON.parse():

var dataHistory = JSON.parse('[[0, "test", "test title", "", []]]');

EDIT

As for the multidimensional array, there are several possibilities:

  1. use the natural position of the elements:

    var dataHistory = [
      ["test", "test title", "", []]    //this is the 0 element
    ];
    var myZeroElement = dataHistory[0];
    
  2. use a key, value structure:

    var dataHistory = [
      {'key':'0', 'value': ["test", "test title", "", []]}
    ];
    var myZeroElement = dataHistory[0].value;
    
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
0

Since both javascript and php have the same two types of quotes to delimitate strings, you need to escape some of them:

echo '<div id="container1" data-history=\'[[0],["test", "test title", "", []]]\' data-current-url="'.$url.'">';
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
-1

This data is not valid JSON string. You can check it here: http://jsonlint.com/

In PHP use json_encode($table); to encode your array to a valid JSON string and put it in your HTML attribute.

Then use JSON.parse(history) in your JS to get the corresponding JSON object.

EDIT: Try to change your php to this:

echo '<div id="container1" data-history="[\"test\", \"test title\", \"\"]" data-current-url="'.$url.'">';

And use the JS code I supplied.

YannPl
  • 1,312
  • 1
  • 14
  • 30