0

I am trying to create a word cloud based on 2 different pieces of code. 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.

The php array code:

<?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 )

while the javascript takes in array in this form:

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?

Any help would be appreciated. Let me know if you need the code for the creation of php array.

JAAulde
  • 19,250
  • 5
  • 52
  • 63
coding_beginner
  • 67
  • 1
  • 11
  • possible duplicate of [Returning JSON from PHP to JavaScript?](http://stackoverflow.com/questions/682260/returning-json-from-php-to-javascript) – epascarello Mar 11 '12 at 12:17
  • @coding_newbie you have to think about your name because you not always be newbie so after be a expert do you like the same name? – Danish Iqbal Mar 11 '12 at 12:20
  • Your sample PHP array doesn't make sense in context of the question. I think I know what you're asking, and JSON alone is not the answer. But the PHP version of your data seems to be missing a lot and clearly doesn't match what you show as the JS version. – JAAulde Mar 11 '12 at 12:22
  • try `json_encode()` - http://php.net/manual/en/function.json-encode.php – Yaniro Mar 11 '12 at 12:22

2 Answers2

1

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. Here's a demo: 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. 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).

JAAulde
  • 19,250
  • 5
  • 52
  • 63
0

You can use json_encode:

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

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

Candide
  • 30,469
  • 8
  • 53
  • 60
  • I think there's more to it, though. The OP seems to be saying he needs to re-arrange the PHP data to match a structure expected by a client side piece of code. After doing that, JSON would be appropriate. Hard to tell, though, given the poor data example given for the PHP side. – JAAulde Mar 11 '12 at 12:26