Thursday, April 23, 2009

How to use Hash at PHP

Is there any way to declare a Hash like an Array at PHP, using a function like array()? I didn't find any function like hash() at PHP yet to declare a hash. But still I know how to make a hash, and use it.

Here is a small piece of code, that will show you how to make a hash. Print the value using key from the hash.
$hash['one'] = 1;
$hash['two'] = 2;
$hash['three'] = 3;

## inspect the variables using var_dump() function

## print a single field from hash
print $hash['one'] . "\n";

You can also use foreach() loop to print all the key/values from the hash easily.
foreach($hash as $key=>$value){
print "$key : $value\n";

But I'm still confused, whether it is a hash or an array? Anyone will take a hand to clarify this issue?


Anonymous said...

To declare a hash, just use

$hash = array('one' => 1, 'two' => 2);

Hashes are "associative arrays" in PHP. "Normal" arrays just have numeric index values.

Anonymous said...

good job!

Anonymous said...

good job

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debug...