如何将嵌套的php数组放入java脚本中


How to put nested php array into java script?

我有这个代码

        $ref = addslashes(htmlspecialchars(json_encode($value)));
        echo '<script>var message_store_'.$key.'=$.parseJSON('.$ref.');alert(message_store_'.$key.');</script>';
        echo '<div class="message_entry" id ="message_entry'.$key.'" onclick="open_up_thread('''.$ref.''','''.$key.''');">'.$key.'</div>';
        $recent = end($value);
        echo '<div id ="recent_message_log_entry'.$key.'"><div>'.$recent['message'].'</div><div>'.$recent['date_posted'].'</div></div>';

我在将数组存储在全局JavaScript变量中时遇到了一个问题。但是我让div onclick事件正常工作,数组被转换为我想要的,函数open_up_thread正在解析编码的JSON对象。

我需要在触发onclick事件之前跟踪该数组(在onclick之后,它很容易,因为它已经转换了)。

那么,我如何在php中将php数组存储为全局javascript对象作为JSON呢。

以及为什么我有onclick工作,但没有变量的任何原因

提前感谢

删除$.parseJSON()函数并删除htmlspecialcharsaddslashes:

$ref = json_encode($value);
echo '<script>var message_store_'.$key.'='.$ref.';alert(message_store_'.$key.');</script>';

JSON是一种可以直接解析为变量的格式。

echo '<script>var message_store_'.$key.'=$.parseJSON('''.$ref.'''); 中的$ref周围加单引号

$ref = json_encode($value);
        echo '<script>var message_store_'.$key.'='.$ref.';alert(message_store_'.$key.');</script>';
        echo '<div class="message_entry" id ="message_entry'.$key.'" onclick="open_up_thread('''.$ref.''','''.$key.''');">'.$key.'</div>';
        $recent = end($value);
        echo '<div id ="recent_message_log_entry'.$key.'"><div>'.$recent['message'].'</div><div>'.$recent['date_posted'].'</div></div>';