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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:41:47+00:00 2026-06-02T21:41:47+00:00

Problem: Trying to use ‘dynamic’ variables to access JSON/JavaScript arrays. # EXAMPLE # Instead

  • 0

Problem: Trying to use ‘dynamic’ variables to access JSON/JavaScript arrays.

# EXAMPLE #

Instead of… array1[ ] or array2[ ] or even array9000[ ]

Somthing like… var my_array = ‘array’ + ‘some generated number’ ;

Then just… my_array[ ] or my_array[some number]

BUT this does’nt work… (sollution NOW at bottom)


I have some PHP that’s reading some directories PLUS there contents to create JSON arrays. The JSON arrays are named sequentially and they are storing image directories…

Example JSON Array Names: images0, images1, images2, etc…

    <?php
        $dir = opendir(getcwd());
        $num=0;
        while($folder = readdir($dir)) {
            if($folder !== '.' && $folder !== '..'){
                if (is_dir($folder)) {
                    echo '<script type="text/javascript">';
                    $folderCHILD = opendir($folder);
                    while($image = readdir($folderCHILD)) {
                        if($image !== '.' && $image !== '..'){
                            $images[]=$folder."/".$image;
                        }
                    }
                    closedir($folderCHILD);
                    echo 'images'.$num.' ='.json_encode($images);
                    unset($images);
                    echo '</script>';
                    $num++;
                }
            }
        }
        closedir($dir);
    ?>

Then I have some Javascript/Jquery that’s ‘attempting’ to access these sequentially named JSON arrays through a click function. The function will create an image using the image directories stored in the JSON arrays.

<script type="text/javascript">
    $(document).ready(function() {
        $('something').click(function() {
            var indexCURRENT = $(this).index();
            var ARRAY = 'images'+indexCURRENT;
            $(document.createElement("img")).attr({ src: ARRAY[some number]}).addClass("image").appendTo('body');
        });
    });
</script>

^The Above Javascipt/Jquery Doesn’t Work^

But Thanks to dev-null-dweller this bit of code does work…

<script type="text/javascript">
    $(document).ready(function() {
        $('#gallery_link p').click(function() {
            var indexCURRENT = $(this).index();
            var ARRAY = 'images'+indexCURRENT;
            $(document.createElement("img")).attr({ src: window[ARRAY][some number]}).addClass("image").appendTo('body');
        });
    });
</script>

The Difference?

.attr({ src: window[ARRAY][some number]})
  • 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-02T21:41:48+00:00Added an answer on June 2, 2026 at 9:41 pm

    Quick answer: your numerated variables should be accessible in window object, accessed like array (window['images1']) so simply changing

    { src: ARRAY[1]}
    

    to

    { src: window[ARRAY][1] }
    

    should make it work.

    Proper answer: use arrays / objects, it’s simple as this:

    <script>
    echo '<script type="text/javascript">';
    echo 'images = [];';
    $folderCHILD = opendir($folder);
    while($image = readdir($folderCHILD)) {
        if($image !== '.' && $image !== '..' && $folder !=='resource'){
            $images[]=$folder."/".$image;
        }
    }
    closedir($folderCHILD);
    echo 'images.push('.json_encode($images).');';
    echo '</script>';
    

    And then:

    { src: images[indexCUR][1] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem trying to use the prependTo() function in jQuery... for
I am experiencing a problem when trying to use the getcontext().prec = 2 function,
I've encountered a rather frustrating problem when trying to use has_and_belongs_to_many associations. The scenario
Ok I have this problem I'm trying to use Jquery to load a partial
i'm new here so this is the problem: I'm trying to use chosen plugin
Trying to use JSTL but have the following problem: Index.xhtml page: <?xml version=1.0 encoding=UTF-8?>
I'm trying to use GSP outside grails and ran to my first problem. I
I'm trying to use jGrowl in an aspx page. But I encountered a problem
I am trying to use different open source apps in my project. Problem is
I'm trying to use eJabberd to host a chatting service. The problem I am

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.