<?php $user = array('id' => 1, 'name' => 'foo'); ?> <script type="text/javascript" charset="utf-8"> var user = <?php echo json_encode($user); ?>; alert(user.length); </script>