1

I want an array in Javascript, structure like this

        $(function()
    {
        // prepare the data
        for (var i=0; i<50000; i++) {
            var d = (data[i] = {});

            d["id"] = "id_" + i;
            d["num"] = i;
            d["title"] = "Task " + i;
            d["duration"] = "5 days";
        }

I want this array to be created through php. I already have the array there created by for loop

EDITED:

Is the above data in Javascript a multidimensional array, a simple array or a var?

is the structure saved in "d" or in data[i][id],data[i][title],... ?

Email
  • 2,395
  • 3
  • 35
  • 63

2 Answers2

3

ie, $data = array('item' => 'description', 'item2' => 'description2');

json_encode($data);

All you need

Lee
  • 20,034
  • 23
  • 75
  • 102
  • 1
    Dumping an array of 50,000 elements onto the page is hardly ideal, but without more info I don't see a better solution. – user229044 Nov 26 '10 at 18:20
  • 2
    I don't think putting 50,000 of anything onto one page using _any_ method is ideal. – Jonah Nov 26 '10 at 18:24
2

Use json_encode() to encode the array.

Access PHP variable in JavaScript That example works with arrays, too.

Community
  • 1
  • 1
Jonah
  • 9,991
  • 5
  • 45
  • 79