Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7616427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:53:04+00:00 2026-05-31T02:53:04+00:00

I am trying to create a word cloud based on 2 different pieces of

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:53:06+00:00Added an answer on May 31, 2026 at 2:53 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a piece of code that replaces one word with
I'm trying to create a word sorting program that will read the words in
I'm trying to create related articles for our news, based on tags (single word
I am trying to create a 2d array in Perl my code: my @wordsList=();
I'm trying to create a word search game (kind of like Wordament but much
I'm trying to create a word ladder program in python. I'd like to generate
I am trying to create a Word document with a very simple word macro.
I am trying to create a bad word filter method that I can call
I trying to create an animation with JQuery that scrolls 1 word in a
I'm trying to create a fairly simple macro in Word 2010 using Visual Basic.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.