gpt4 book ai didi

javascript - 将 php 数组导入 javascript 数据集

转载 作者:行者123 更新时间:2023-11-29 18:07:04 26 4
gpt4 key购买 nike

我提取了一些数据并将它们组织在 .php 文件中的数组中。

现在我想在另一个扩展名为 .html 的文件中使用 d3 方法可视化该数据。我想将该数组导入数据集中。

我将尝试在此处发布代码或有意义的内容:

<?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 />";
}

?>

foreach 的输出是:

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

当我执行 echo json_encode($newArray,JSON_PRETTY_PRINT);输出是:

{
"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
}

我试过:

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

但我无法获取数据以供使用。

我好像跑题了,但我的问题是如何将这些数据导入到我的 html 文件中。 (当然是同一个文件夹)

最佳答案

您可以将数据保存在文本文件中并进行解析。

var jsonphp = '<?php echo $d = file_get_contents("json.txt"); ?>';
var jsondata = $.parseJSON(jsonphp);

关于javascript - 将 php 数组导入 javascript 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30428674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com