0

I have a php array of objects istantiated from the following class:

class my_class {
    public $id;
    public $timestamp;
}

These are stored in an array with indexes 0,1,2.....

I am getting this array with jquery through the Session variable and I attempt to print it in the following ways:

var myclass_map = "<?php $_SESSION['myclass_map']?>";
$.each(myclass_map, function(key, value) {
    console.log(key + ' ' + value["id"]);
});

and

var track_map = "<?php $_SESSION['myclass_map']?>";
$.each(myclass_map, function(key, value) {
    console.log(key + ' ' + value.id);
});

but both give me an undefined reference to value while the print out the key. How can I access the objects variables?

5
  • 3
    You are confusing asp with php - 2 different things. Commented Aug 20, 2012 at 12:19
  • Besides do you really need jQuery to loop thourgh an array?? Commented Aug 20, 2012 at 12:22
  • Unfortunately I need it. I also Edited the question, using php this time! Commented Aug 20, 2012 at 12:27
  • You have some strange PHP version installed on your system. Commented Aug 20, 2012 at 12:30
  • I see your point, I edited again. Commented Aug 20, 2012 at 12:43

1 Answer 1

4

At the very begining of your JS script, print the array converted to js format, using json_encode.

var track_map = <?=json_encode($map)?>;
Sign up to request clarification or add additional context in comments.

1 Comment

it seems as the correct approach, but isn't there any quotes or something similar missing? I am using the following : var map = "<?php echo json_encode($_SESSION['map'] )?>";

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.