views:

63

answers:

4

I am trying to store values in php, like a HashTable with multiple keys. For example I am I would want this to return two different values:

$value1=$content['var1']['var2']['var3']['typea'];

$value2=$content['var1']['var2']['var3']['typeb'];

What would be the best way to go about implementing a feature like this?

+1  A: 

You can set values the same way you get them.

$content['var1']['var2']['var3']['typea'] = $value1;
$content['var1']['var2']['var3']['typeb'] = $value2;
PMV
A: 

You can take advantage of PHP being a dynamic scripting language by letting it create your desired array structure automatically:

 $content['var1']['var2']['var3']['typea'] = "value1";
mario
+2  A: 

Instead of building a complicated array, how about you define and use a simple class instead? e.g:

<?php
class beer {
  var $brand;
  var $ounces;
  var $container;
}

$mybeer = new beer();
$mybeer->brand = "Coors";
$mybeer->ounces = 12;
$mybeer->container = "can";

echo $mybeer->brand;
echo $mybeer->ounces;
echo $mybeer->container;
?>
Banjer
A: 

One way would be to make a unique key based on the various keys and store it in one big array.

Instead of this,

$value1=$content['var1']['var2']['var3']['typea'];

you'd have something like this...

$contentKey = generateKey("var1", "var2", "var3", "typeA");
$value1 = $content[$contentKey];

where generateKey would do something like hash the various inputs, or concatenate them together with some unique delimiter like a quadruple underscore. This would require fewer array lookups than the other solutions, and (in my opinion) is easier to read.

voodoogiant