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

  • Home
  • SEARCH
  • 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 7773719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:11:50+00:00 2026-06-01T17:11:50+00:00

I am working on a WordPress plugin and I need to pass PHP array

  • 0

I am working on a WordPress plugin and I need to pass PHP array to Javascript array. I have tried using join(), implode() and even Json_encode. But, the wordpress is not displaying any value.

When using join(), I used the code:

<?php
$php1 = array(1,2,3);

?>
<script language='Javascript'>
var lat = ["<?php echo join("\", \"", $php1); ?>"];
document.write(lat[1]);
</script>

If used on localhost(without wordpress), the above code provides a valid output. But, somehow, its not working on WordPress. The “apache error log” show this message:

PHP Warning: join() [function.join]: Invalid arguments passed in \wp-content\plugins\Animation\animation.php on line 129, referer: http://localhost/Website/wp-admin/options-general.php?page=js

Same is the case with implode(). Server error log shows same above warning for implode().

Then I tried for json_encode using the code below:

var lat = <?php echo json_encode($php1); ?>;

But the no value is returned.

Edit: Code I used for JSON:

<?php
/*
Plugin Name: PHPToJavascript
*/
$arr = array(1,2,3,4,5,6,7,8,9);            //array to pass

add_action('admin_menu','admin_jav');

function admin_jav(){
add_submenu_page('options-general.php','Javarray','Javarray','manage_options','javarray',jav_handler);
}

function jav_handler(){
echo 'Into handler';

?>
<SCRIPT LANGUAGE = 'Javascript'><!--
var sm=<?php echo json_encode($arr); ?>;      //using Json  

document.write(sm[1]);                       //doesnt display any output!!!

</SCRIPT>
<?php
}
?>

Please guide me through this. I appreciate any help. It would be great if you help me in passing this PHP array to javascript 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-06-01T17:11:52+00:00Added an answer on June 1, 2026 at 5:11 pm

    The $arr variable is out of scope. If you want to use the global $arr variable, you need to modify your jav_handler() function to bring the variable into local scope:

    function jav_handler(){
        global $arr;
        // ...
    

    However, it’s good practice to always avoid global variables when you can, so the preferred way of doing this is to change the function to take the array as an argument and pass it explicitly when calling the function:

    function jav_handler($arr){
        // ...
    }
    
    jav_handler($arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on the admin section of a Wordpress plugin and I need
I'm working on a wordpress plugin, and have come across a barrier of sorts.
I have one instance of Wordpress Multiuser and need ShareThis plugin to be automatically
I have a custom developed WordPress plugin that is using jQuery 1.4 and for
I'm working on a wordpress theme-switching plugin that relies on the on a php
I'm working on a WordPress plugin. At the admin page, I have a ajax
I am currently working on a plugin for Wordpress involving lots of .php files.
I'm working in new Wordpress plugin, but I need help with one regular expression.
I'm working with a WordPress plugin. It adds a flash player to the output
I am working on a wordpress plugin which allows user to enter name of

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.