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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)