doujiao1948 2015-05-24 21:46
浏览 43
已采纳

将php数组导入javascript数据集

I have extracted some data and organized them in an array in .php file.

Now I want to visualize that data using d3 methods in another file with .html extension. I would like to import that array into the dataset.

I'll try to post the code or what makes sense here:

<?php

        // data extraction from a bunch of xml files.
        // stored all of that in an array ---- $topics 
        // sorted the array by frequency of the elements 

        $topic_count = array_count_values($topics);

        echo '<p>' . "Array Testing " . '</p>';

        foreach ($newArray as $key => $value) {
                echo "$key - <strong>$value</strong> <br />"; 
        }

    ?>

The output from the foreach is:

tim harper published - 5
TIM HARPER Published - 3
Updated - 1
Illustration Garnotte - 1
cour rejette - 1
cour supr me rejette les peines obligatoires du gouvernement harper - 1
Chris Wattie - 1
Stephen Harper - 1
contenu publication - 1
information - 1
juif canadien - 1

When I do echo json_encode($newArray,JSON_PRETTY_PRINT); the output is:

{
    "tim harper published": 5,
    "TIM HARPER Published": 3,
    "Updated": 1,
    "Illustration Garnotte": 1,
    "cour rejette": 1,
    "cour supr me rejette les peines minimales obligatoires du gouvernement harper": 1,
    "Chris Wattie": 1,
    "Stephen Harper": 1,
    "contenu publication": 1,
    "information": 1,
    "juif canadien": 1
}

I have tried doing:

script type = "javascript">
            var dataset = <?php echo json_encode($newArray,JSON_PRETTY_PRINT); ?>;
        </script>

but I can't get the data to use.

Seems like I'm getting out of topic here, but my question is how can I import that data in my html file. (same folder ofcourse)

  • 写回答

1条回答 默认 最新

  • doushi1900 2015-05-24 21:56
    关注

    You can save the data in a text file and parse it.

    var jsonphp = '<?php echo $d = file_get_contents("json.txt"); ?>';
    var jsondata = $.parseJSON(jsonphp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制