Array And Object in PHP and Javascript

When you have had some experience of javascript programming, you must know that associative array is an object and an object is an associative, at least property wise (associative array is very different from index based array in many aspects)they are similar.

Example 1

  var obj = {};
   obj.f1 = 1;
   obj.f2 = 2;
   alert(obj.f1); // displays ‘1’ 
   alert(obj['f1']); // displays ‘1’

   var arrayobj = [];
   arrayobj['f1'] = 1;
   arrayobj['f2'] = 2;
   alert(arrayobj.f1); // displays ‘1’
   alert(arrayobj['f1']); // displays ‘1’

So you can see that there is no difference between an associative array and an object in javascript. The similar concept exists in php too, but there is something that is different between this two languages.

Difference is in php, array and object are different entities, $obj->f1 does not automatically lead to $obj[‘f1’], you will need to do a conversion before you can use it the same way as you do in javascript

Example 2

$obj = new stdClass();
$obj->f1 =1;
$obj->f2 =2;

echo $obj->f1;
 
$arrayobj = (array)$obj; // conversion

echo $arrayobj['f1'];

Vise versa Example 3

$ar = array();
$ar['f1'] =1;
$ar['f2'] =2;

echo $ar['f1'];

$objarray=(object)$ar;  

echo $objarray->f1;
 

It is very useful tool in php that you can make conversions such as this, so it is not a big decision to use array or object to organize data.

A few other confusing things in php and javascript

1. stdClass and named classes in php

stdClass(anonymous objects ) is pretty much like the prototype of all objects (new object() or {} in literal) in javascript, both languages have a unique feature that other typed languages do not have, that is you can add properties and methods to instances of stdClass or objects in javascript any time and anywhere.

In php dynamic properties are done through php overloading, these dynamic entities are processed through magic methods, like : __set() and __get() , you do not have to override them to get the dynamic properties, but you can override them to change the behaviour of dynamic properites

You can create stdClass through normal instantiation of stdClass, another popular way of getting stdClass is through type casting as we discussed above(Example 3).

Named classes are customized defined classes like myClass {…} it also has dynamic properties capability.

2. Object type in Javascript

Javascript also support dynamically created properties and methods any time, but only for object types as in example 4, not primitive types or value types like int, boolean and string(example 5).

There is a type cast when these types are used as different types(an int used as string or a string used as int), but in assignment value types and object types variables are immutable, that is why you cannot add a field to an integer variable.

Example 4


var c = 0;
c.f1 = 1; // no exception, but undefined

Example 5


var c = {};
c.f1 = 1; 

3.Eval() function in Javascript
Eval is a terrible powerful function, what it does is to run the argument(a string) as a piece of javascript, do whatever this script tells me to do…
If the string is a jason string, then it is better to use tools like jQuery.parseJSON

This entry was posted on Friday, January 28th, 2011 at 1:01 am and is filed under Javascript, PHP, Software Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Array And Object in PHP and Javascript”

  1. Thanks for sharing this information. I save this post and will visit it again to implement this code.

Leave a Reply

*