difference between javascript array and php array

I love php array because the array’s key can not only be number but also be string.

$arr=array("a","b","c");
$arr=["a","b","c"];
$arr=array("k1"=>"a","k2"=>"b","k3"=>"c");
$arr=["k1"=>"a","k2"=>"b","k3"=>"c"];

 

That makes it an association array or a map in other programming languages. The associative array or map is useful for lookup. Given a key, you do not need to search the whole array to get the value. Just use arr[key] to get the value for the key. To understand what I mean, consider a programming language that does not support associative array such as javascript. You have an array of objects who have two properties:

function MyObj(a,b)
{
    this.a=a;
    this.b=b;
}

var obj1=new MyObj("k1","a");
var obj2=new MyObj("k2","b");
var obj3=new MyObj("k3","c");

var arr=[obj1,obj2,obj3];

var tofind="k3";
for(var i=0;i<arr.length;i++)
{
    if(arr[i].a==tofind)
    {
        alert("find obj whose first property is "+tofind+", its second property is "+arr[i].b);
        break;
    }
}

You need to iterate over the array to find the object with particular feature(i.e., one of its properties has the specific value). Javascript does not support associative array, i.e., javascript has only indexed array whose key is number. You can only access its element using a number index,i.e.,arr[1]. If you use a string index to access its member like arr[“1″], the string index “1” would be converted to integer 1. The associative array in php makes the element lookup easier and quicker. When we store objects in an array, we use the value of an object’s property(the property we know would be searched for in the future)  as the key.

<?php

class MyObj
{
  var $a;
  var $b;

  function __construct($a,$b)
  {
    $this->a=$a;
    $this->b=$b;
  }
}

$obj1=new MyObj("k1","a");
$obj2=new MyObj("k2","b");
$obj3=new MyObj("k3","c");

$arr=["k1"=>$obj1,"k2"=>$obj2,"k3"=>$obj3];

$tofind="k3";

echo $arr[$tofind]->b;

Now if we want to find the object in the array whose property a has the value “k3″,  we can use the value as the key to find the target object. No need to iterate over the array.

In javascript, we can use an object to simulate an associative array:

function MyObj(a,b)
{
    this.a=a;
    this.b=b;
}

var obj1=new MyObj("k1","a");
var obj2=new MyObj("k2","b");
var obj3=new MyObj("k3","cc");

var arr={};
arr["k1"]=obj1;
arr["k2"]=obj2;
arr["k3"]=obj3;
var tofind="k3";
alert(arr[tofind].b);

Note that we use bracket notation to access object property like accessing array’s member but that does not mean arr is changed an array. arr.length would be “undefined”. Why use bracket notation instead of dot notation? That is because if the key is  a number(or a string representing a number), we can not access the property using dot notation. Javascript does allow to use an identifier, a number, or a quoted string for the property name of an object (the identifier and number will be converted to a string for the property name) but dot notation is only allowed to access the property whose name is a legal identifier while bracket notation has no such limitation(see also this and this).

modern browsers(IE11+) support the Map object in javascript that is equivalent to the associative array. Here is a tutorial about the new Map object.

Comments are closed, but trackbacks and pingbacks are open.