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],... ?
asked Nov 26, 2010 at 18:15
Email
2,4353 gold badges36 silver badges64 bronze badges
-
1And please mark the best answer to your question here.Jonah– Jonah2010年11月26日 18:26:41 +00:00Commented Nov 26, 2010 at 18:26
-
sorry now i will. i did not know so far, thx 4the hintEmail– Email2010年11月26日 18:40:26 +00:00Commented Nov 26, 2010 at 18:40
2 Answers 2
ie, $data = array('item' => 'description', 'item2' => 'description2');
json_encode($data);
All you need
answered Nov 26, 2010 at 18:17
Lee
21.3k23 gold badges78 silver badges103 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user229044
Dumping an array of 50,000 elements onto the page is hardly ideal, but without more info I don't see a better solution.
Jonah
I don't think putting 50,000 of anything onto one page using any method is ideal.
Use json_encode() to encode the array.
Access PHP variable in JavaScript That example works with arrays, too.
answered Nov 26, 2010 at 18:18
Jonah
10.1k5 gold badges49 silver badges80 bronze badges
4 Comments
rhino
Sorry, I don't get you. What do you mean by saying "it's quicker"?
Jonah
Linking to your answer, instead of writing it again. :) </offtopic>
Email
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); ?>;
Jonah
Any array, string, or integer can be passed through. Json_encode takes care of the rest.
default