Assign a PHP Array to a Javascript Array?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
New php-forum User
New php-forum User
Posts: 62
Joined: Fri Oct 11, 2002 6:53 am

Assign a PHP Array to a Javascript Array?

Postby imroue » Sat Nov 09, 2002 10:55 pm

Is it possible to assign values of a PHP array dynamically to a Javascript Array?
:?: :?: :?:

If so, how?

Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Nov 10, 2002 12:46 am

Code: Select all



echo "<script language=\"JavaScript\">
imgName = new Array()
imgName[0] = \"$1img\"
imgName[1] = \"$2img\"

New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 13, 2002 9:53 am

Postby CFDev » Wed Nov 13, 2002 10:34 am

Here's an example from ActivEdit, where we first check for an attribute, then instantiate a JavaScript array and load the array with values from the PHP variable (attribute).

Code: Select all


//--- Set Quick Format options.
   if (!isset($this->attributes["quickformatnames"])) {
      $this->attributes["quickformatnames"] = array("Normal","Formatted","Address","Heading 1","Heading 2",
      "Heading 3","Heading 4","Heading 5","Heading 6","Numbered List","Bulleted List",
      "Directory List","Menu List","Definition Term","Definition","Paragraph");
   } else {
      $this->attributes["quickformatnames"] = explode(",", $this->attributes["quickformatnames"]);


<script language="JavaScript">

var quickformatNameArray = new Array();

while (list ($key, $val) = each ($this->attributes["quickformatnames"])) {
        $generatedContent .= "quickformatNameArray[$key] = '$val';";


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 4 guests