SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2012
    Location
    California
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Associative Arrays

    I've just started studying PHP and I came across something called associative arrays. It allows you to do the following:

    PHP Code:
    $soccerTeam = array();
    $soccerTeam['andy'] = 10
    The equivalent in JS would be:

    Code:
    var soccerTeam = new Array();
    soccerTeam['andy'] = 10;
    I tried this out in JS in FireBug and it seemed to work. My only question is, is it a good idea? I could not find any JS documentation on it and I even found an article that said it was a bad practice. And if it is bad in JS, is it bad in PHP as well?

    Thanks!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    In JavaScript associative arrays are called Objects and almost the entire language uses them for almost everything.

    Using new Array() is unnecessary though - you can define an ordered array using [] and an associative one using {} after the = in JavaScript.

    Code:
    var soccerTeam = {};
    soccerTeam['andy'] = 10;
    or to shorten it further:

    Code:
    var soccerTeam = {andy:10};
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    n JavaScript associative arrays are called Objects and almost the entire language uses them for almost everything.

    Code:
    var soccerTeam = {};    // this is an object
    soccerTeam['andy'] = 10
    
    alert(soccerTeam['andy']);
    alert(soccerTeam.constructor);
    
    var soccerTeam = [];     // this is an array
    soccerTeam['andy'] = 10
    
    alert(soccerTeam['andy']);
    alert(soccerTeam.constructor);
    all arrays are objects, all objects are not arrays

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by vwphillips View Post
    all arrays are objects, all objects are not arrays
    That's the difference between associative arrays and ordinary arrays. In JavaScript any associative array you define is actually just defined as an object ans ANY object can be referenced as if it were an associative array.

    JavaScript does not have a separate data type for associative arrays and any associative arrays you attempt to create will just create an object.

    Note that an Arrayi is something completely different from an associative array and that none of the Array methods can be used on an associative array in JavaScript because those methods only work for Arrays and in JavaScript an associative array is an Object and not an Array.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •