Php hash 和数组,PHP中的Array和Hash有什么區別?

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.

你描述的東西都是數組。兩者之間的唯一區別是您明確設置第二個鍵的鍵,因此它們被稱為關聯數組。我不知道你從哪里獲得了Hash術語(Perl?),但這不是他們在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.

正如您可能期望的那樣,打印$ bar ['one']將輸出uno,並且從第一個示例執行$ foo [0]將輸出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.

就函數而言,PHP函數大部分時間都會使用這些“類型”數組中的任何一個並按照你想要的那樣做,但是要注意區別,因為有些函數會對你的索引做一些時髦的事情。有些人不會。通常最好在使用數組函數之前閱讀文檔,因為它會根據數組的鍵來記錄輸出。

You should read the manual for more information.

您應閱讀手冊以獲取更多信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值