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],... ?

2
  • 1
    And please mark the best answer to your question here. Commented Nov 26, 2010 at 18:26
  • sorry now i will. i did not know so far, thx 4the hint Commented Nov 26, 2010 at 18:40

2 Answers 2

3

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

json_encode($data);

All you need

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

2 Comments

Dumping an array of 50,000 elements onto the page is hardly ideal, but without more info I don't see a better solution.
I don't think putting 50,000 of anything onto one page using any method is ideal.
2

Use json_encode() to encode the array.

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

4 Comments

Sorry, I don't get you. What do you mean by saying "it's quicker"?
Linking to your answer, instead of writing it again. :) </offtopic>
could you give an example in php how the structure of the array has to look to fit into the Javascript. The javascript part as i understood your answer is simply echoing var d=<?php echo json_encode($data); ?>;
Any array, string, or integer can be passed through. Json_encode takes care of the rest.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.