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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:50:41+00:00 2026-05-30T13:50:41+00:00

In PHP, if you do this $var = array(); $var[5000] = 1; echo count($var);

  • 0

In PHP, if you do this

$var = array(); 
$var[5000] = 1;
echo count($var);

It prints 1.

In JS it makes the “missing” elements.

<script type="text/javascript">
    var fred = [];
    fred[10] = 1;
    alert(fred.length);
</script>

Alerts “11”.

Can I prevent this? Is it good for anything? I’m a PHP coder trying to adapt to Javascript.

EDIT:

I’m working on an app that uses Google Maps v2 and markerManager. The code has been working for ages, but a problem has suddenly arisen in Chrome (17.0.963.56 m) where markers seem to be duplicated (or more) and then rendering moved markers goes completely wrong, often followed by a browser freeze-up. Looking in the DOM using Firebug, I found gazillions of “undefined” elements in arrays under the grid_ variable in markerManager. I figured that, if I could remove these, I might have slicker code, whether it fixes the marker problem or not. Thanks.

  • 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-30T13:50:43+00:00Added an answer on May 30, 2026 at 1:50 pm

    As a PHP coder, you’re accustomed to array keys being fairly arbitrary. In JavaScript however, if you assign an array element numerically that doesn’t exist, the array will be allocated with empty elements up to the one you created. That’s the behavior you found. Objects, on the other hand, denoted by {} rather than [], accept arbitrary property names (with caution) and function a little more closely to how you’re accustomed PHP’s array structures functioning. This is not to say that JavaScript objects (or object literals) are a direct analogue to PHP arrays….

    If what you want is a data structure with a key called “10”, that is a job for an object literal (though it isn’t great practice to name object properties numerically.

    var fred = {};
    fred["10"] = 1;
    

    Note, the more normal syntax of dealing with an object literal is the object.property notation, but that is not valid for numeric properties:

    // name as a property
    fred.name = "fred";
    
    // Syntax error...
    fred.10 = 10;
    
    // Use [] notation for numeric properties:
    fred["10"] = 10;
    

    Object literals do not have a length property, however. So you cannot do:

    fred.length;
    // undefined
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is how I got my php array to javascript echo 'var daysofweek =
This is how I got my php array to javascript echo 'var daysofweek =
I have this small PHP script: <?php session_start(); $var = array(); $var['key'] = 'Var
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
So I call this PHP script from the command line: /usr/bin/php /var/www/bims/index.php projects/output and
Is it it possible to do this in php? Javascript code: var a =
I'm trying to pass this to a PHP script through AJAX: var answers={}; for
$(.delete).click( function() { var thesender = this; $(thesender).text(Del...); $.getJSON(ajax.php, {}, function(data) { if (data[result])
I am trying to assign a PHP array into a javascript variable like this:
in my app i need to send an javascript Array object to php script

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.