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

2
  • 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. Commented Nov 26, 2010 at 18:20
  • 2
    I don't think putting 50,000 of anything onto one page using any method is ideal. Commented Nov 26, 2010 at 18:24
2

Use json_encode() to encode the array.

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

4
  • Sorry, I don't get you. What do you mean by saying "it's quicker"? Commented Nov 26, 2010 at 18:37
  • Linking to your answer, instead of writing it again. :) </offtopic> Commented Nov 26, 2010 at 18:38
  • 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); ?>; Commented Nov 26, 2010 at 19:01
  • Any array, string, or integer can be passed through. Json_encode takes care of the rest. Commented Nov 26, 2010 at 22:28

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.