简体   繁体   English

将php数组转换为javascript数组

[英]converting php array into javascript array

I am trying to create a word cloud based on 2 different pieces of code. 我正在尝试根据2个不同的代码段创建词云。 One returns an array with the words and the weight for each word while the second piece takes in this array and creates the word cloud. 一个返回一个包含单词和每个单词的权重的数组,而第二个则采用该数组并创建单词云。 However, I am not sure how to convert the php array to a javascript array. 但是,我不确定如何将php数组转换为javascript数组。

The php array code: php数组代码:

<?php
$arr = *data from database*;
$string = implode(",", $arr);
error_reporting(~E_ALL);
$count=0;
//considering line-return,line-feed,white space,comma,ampersand,tab,etc... as word separator
$tok = strtok($string, " \t,;.\'\"!&-`\n\r");
if(strlen($tok)>0) $tok=strtolower($tok);
    $words=array();
    $words[$tok]=1;

    while ($tok !== false) {
        //echo "Word=$tok<br />";
        $tok = strtok(" \t,;.\'\"!&-`\n\r");
        if(strlen($tok)>0) {
            $tok=strtolower($tok);

            if($words[$tok]>=1){
                $words[$tok]=$words[$tok] + 1;
            } else {
                $words[$tok]=1;
            }
        }
    }

This returns an array such as this when echoed. 当回显时,它将返回这样的数组。 Array ( [checking] => 1 ) 数组([checking] => 1)

while the javascript takes in array in this form: 而javascript采用这种形式的数组:

var word_list = [
          {text: "Lorem", weight: 15},
          {text: "Ipsum", weight: 9, url: "http://jquery.com/", title: "I can haz URL"},
          {text: "Dolor", weight: 6},
          {text: "Sit", weight: 7},
          {text: "Amet", weight: 5}
          // ...other words
      ];

How do I for loop the php array to replace into the text and weight variables? 我如何for循环php数组以替换为text和weight变量?

Any help would be appreciated. 任何帮助,将不胜感激。 Let me know if you need the code for the creation of php array. 让我知道您是否需要用于创建php数组的代码。

This should do it (placed after your current code): 应该这样做(放在您当前的代码之后):

$cloud = Array();
foreach ($words as $text => $weight) {
    $cloud[] = Array('text' => $text, 'weight' => $weight);
}
echo json_encode($cloud);

It doesn't account for the possibility of url , title , etc in your given JS sample, but I think it's what you're after. 它不考虑给定JS示例中urltitle等的可能性,但是我认为这是您所追求的。 Here's a demo: http://codepad.org/OnEDIe1l 这是一个演示: http : //codepad.org/OnEDIe1l

Of course, you could just adjust the PHP which builds your $words array to build it such that it already matches what my code produces in $cloud . 当然,您可以调整构建$words数组的PHP来构建它,使其已经与我的代码在$cloud生成的内容匹配。 But maybe you need to do other things with $words ... (and I can see how it's easier to maintain a count during tokenization with the code you currently have). 但是也许您需要使用$words来做其他事情...(而且我可以看到在使用您当前拥有的代码进行标记化期间维护计数变得更加容易)。

You can use json_encode : 您可以使用json_encode

$a = array("a" => 1, "b" => 2);
print json_encode($a);

output: {"a":1,"b":2} 输出: {"a":1,"b":2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM