tags:

views:

725

answers:

2

What is differents from an arrangement in a hash in PHP?

An arrangement: array(1,2,3...) A hash: array(key1=value1, key2=value2, ...)

Or will it be a thing to treat as the totally same thing?

※ For example, will the function arguments allows array be effective for the hash?

Because I distinguish it by the conventional language and used it, I am puzzled.

+11  A: 

Both the things you are describing are arrays. The only difference between the two is that you are explicitly setting the keys for the second one, and as such they are known as associative arrays. I do not know where you got the Hash terminology from (Perl?) but that's not what they are referred as in PHP.

So, for example, if you were to do this:

$foo = array(1,2,3,4,5);
print_r($foo);

The output would be:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

As you can see, the keys to access the individual values you put in were created for you, but are there nonetheless. So this array is, essentially, associative as well. The other "type" of array is exactly the same way, except you are explcitily saying "I want to access this value with this key" instead of automatic numeric indexes (although the key you provide could also be numeric).

$bar = array('uno' => 'one', 'dos' => 'two');
print_r($bar);

Would output:

Array
(
    [uno] => one
    [dos] => two
)

As you might then expect, doing print $bar['one'] would output uno, and doing $foo[0] from the first example would output 1.

As far as functions go, PHP functions will most of the time take either one of these "types" of array and do what you want them to, but there are distinctions to be aware of, as some functions will do funky stuff to your indexes and some won't. It is usually best to read the documentation before using an array function, as it will note what the output will be depending on the keys of the array.

You should read the manual for more information.

Paolo Bergantino
plus uno :P ..
alex
muchas gracias senor :)
Paolo Bergantino
The internal data type that's "driving" php's arrays is called HashTable.
VolkerK
A: 

In actuality, there are no arrays in php - there are only associative arrays (which is basically a hash table)

Try doing

$ar=array("zero","one","two","three","four");
unset($ar[3]);

doing so will remove "three" from the array, but you'll notice that the array keys (the array is not associative) will remain the same (0,1,2,4) - in any normal language it would renumber the key for "four" to 3.

strubester