Four Flavors of Associative Arrays

A guy in a blue shirt speaking.

When it comes to programming, there are a lot of different ways to accomplish the same task. Here are four different ways to create associative arrays in PHP, C# and JavaScript:

PHP Array Method (this is very common in PHP):

$user = array();
$user[“name”] = “John Smith”;
$user[“email”] = “john@smith.com”;

PHP Custom Built Object:

class User
{
public $name;
public $email;
}

$user = new User();
$user->name = “John”;
$user->email = “john@smith.com”;

C# Built in Generic Type – Dictionary:

Dictionary user = new Dictionary<string, string>();
user.Add(“name”, “John”);
user.Add(“email”, “john@smith.com”);

JavaScript Standard Associative Array w/ Array.push() method:

var user = [];
user.push({
name: “John”,
email: “john@smith.com”
});

Leave a Reply

Your email address will not be published. Required fields are marked *