1

I am having som trouble with passing a multidimensional and associative array from php to Javascript. I converte it with JSON, using:

_SESSION =  '<?php print json_encode($_SESSION) ?>';

I have also tried

_SESSION =  '<?php print json_encode($_SESSION, JSON_PRETTY_PRINT) ?>';
_SESSION =  $.parseJSON('<?php print json_encode($_SESSION) ?>');

both of them give me errors like: Uncaught SyntaxError: Unexpected token ILLEGAL.

However, the first one doesn't give me any errors and I can access it in Javascript. It then outputs:

{"items":{"221163":{"CodeComplete":null,"Project":"Coding","Team":"Mail","TimeSpent":25","Children":[]}}, {"221165":{CodeComplete":null,"Project":"Coding","Team":"Batman","TimeSpent":"40","Children":[]}}

I belive this is like a string, since _SESSION[0] outputs "{". However, I want it to be an array or an object. The Array looks like this in php:

_SESSION( "items" => array(
                        221163 =>
                                 array( CodeComplete => null
                                        Project => "Coding"
                                        Team => "Mail"
                                        Timespent => "25"
                                        Children => array(
                                                           )
                                      )
                        221165 =>
                                 array( CodeComplete => null
                                        Project => "Coding"
                                        Team => "Stones"
                                        Timespent => "40"
                                        Children => array(
                                                           )
                                       )

                           )             
        )

I want to be able to access this array in the same way as I can in php (not litterary ofcorse) but _SESSION["items"] or _SESSION.items is undefined as _SESSION is a string...

Any ideas of what I'm doing wrong?

1
  • 3
    _SESSION = '<?php print json_encode($_SESSION) ?>'; ... remove the apostrophes so _SESSION = <?php print json_encode($_SESSION) ?>; Commented Oct 9, 2015 at 14:37

1 Answer 1

0

Just as @CD001 suggested in his comment, I removed the '' from the transfer so that it became

_SESSION = <?php print json_encode($_SESSION) ?>; 

instead and now everything works fine, thank you!

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.