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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:34:36+00:00 2026-06-04T21:34:36+00:00

I have an array im trying to loop through to create a new type

  • 0

I have an array im trying to loop through to create a new type of array specific to my current page.

My array looks like this:

 //$_SESSION['data'] =
 Array ( 
        [0] => 1 
        [1] => 0 
        [2] => Tom 
        [8] => 1 
        [4] => 1 
        [5] => Array ( 
                        [7] => Array ( 
                                    [0] => Andrew 
                                    [1] => 1 
                                    [2] => 1 
                                    [4] => 0 
                                    [5] => avatar.jpg 
                                    [6] => 1 
                                    ) 
                    ) 
        [6] => Array ( [0] => 1 
                       [1] => 2 
                     ) 
 )

So in my JS file i have this:

var stats = <? echo json_encode($_SESSION['data'][5]); ?> ; //this is the array 

my_data = new Array();
for(var key in stats){

    if(key in my_data){} else { //prevent double entry

    my_data[key] = new Array();
    my_data[key][0] = stats[key][6]; 
    my_data[key][1] = stats[key][5];
    my_data[key][2] = stats[key][2]; 
    my_data[key][3] = stats[key][0];
    }
}

console.log(my_data);

Now in console.log i get this :

[undefined × 7, Array[4]
 0: "1"
 1: "avatar.jpg"
 2: "1"
 3: "Andrew"
 length: 4
 __proto__: Array[0]
 ]

I’m wondering why it is saying undefined x7?

  • 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-04T21:34:38+00:00Added an answer on June 4, 2026 at 9:34 pm

    Why not use JSON??

    var stats = <? echo json_encode($_SESSION['data'][5]); ?> ;
    

    YOu can now iterate normally.

    for (x in stats){
         console.log(stats[x]);
    }
    

    UPDATE

    Yes, you will get ‘x7 undefined’. This is because you are directly setting the 8th entry in the js array. (key == 7 in the iteration of for-loop) First 7 entries are essentially undefined.

    If you want to use the first few indices, use array.push(..) or manually set the last index using array[array.length] = new_object;

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

Sidebar

Related Questions

I am trying to write PHP code to loop through an array to create
I am trying to loop through only a specific sub array in PHP with
I have a 3x3 array that I'm trying to create a pointer to and
I store post IDs in an array. I would like to loop through the
I have data setup like this.. Transaction   -Name   -ID   -Amount   -Tags <-- Array of
Trying to loop through an array of hashes and getting an error message that
I am trying to loop through an array and perform an httpwebrequest in each
Hello I am trying to create a for loop that loops through the linked
I have variable names stored in an array, and I want to loop through
I have an array of URL Links that I'm trying to load into a

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.