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 7654669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:18:59+00:00 2026-05-31T12:18:59+00:00

so I am a total php/javascript noob. I’m trying load a full php array

  • 0

so I am a total php/javascript noob.

I’m trying load a full php array into a javascript array.
I wrote this for Javascript:

    var names = new Array();
for(var i = 0; i < 48; i++) {
    names[i] = "<?php echo giveJS() ?>";
}

And this for php

static $counter = 0;

function giveJS() {
    global $names;
    global $counter;
    $counter++;
    return $names[$counter]; 
}

I already checked if the php array is correctly filled with data.
When I write a output line in javascript, like

document.write(names[10]);

it only gives me the first entry in the php array so it seems like everytime the for loop is repeated its initialising the php file from scratch and so setting the counter to 0.
How can i fix that?

  • 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-31T12:19:00+00:00Added an answer on May 31, 2026 at 12:19 pm

    In php

    //Bla being the php array you want to give to javascript. Create it however you like
    $bla = array();
    $bla[] = 'cat';
    $bla[] = 'dog';
    $bla[] = 'bat';
    echo '<script>var myarray = '.json_encode($bla) .';</script>';
    

    The above code will then output a script tag contain a varible called myarray, then contents of which will be JSON specifying the above array (json_encode formats the array in to javascript internal syntax- the array will probably look like [‘cat’,’dog’,’bat] )

    You can then get values from the JavaScript array as so:

    <script>
     console.log(myarray[2]);
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Total noob question. In PHP I can easily do this: foreach( $array1 as $key
I'm a total newbie and want to start with php. I know some javascript
Total newbie question but this is driving me mad! I'm trying this: myInt =
Total noob to anything lower-level than Java, diving into iPhone audio, and realing from
I have the following javascript into a index.php file: <script type=text/javascript> $('.showpics, .showpics2').live('click', function()
I using the price in Euro in JavaScript: var currency = '\u20AC'; Total Sum
I am extremely new to PHP and, although I am quite familiar to javascript,
In a header file i have which is populated by PHP: <script type=text/javascript> var
I am trying to pass a variable from evercookie to PHP using New Image()
There is a way to get the total memory PHP is using ( memory_get_usage()

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.