php php中有字典吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6490482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:32:51  来源:igfitidea点击:

Are there dictionaries in php?

phpdictionaryassociative-arrayterminology

提问by bzupnick

For example:

例如:

$names = {[bob:27, billy:43, sam:76]};

and then be able to reference it like this:

然后可以像这样引用它:

 $names[bob]

回答by Jacob

http://php.net/manual/en/language.types.array.php

http://php.net/manual/en/language.types.array.php

<?php
$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];
?>

Standard arrays can be used that way.

可以以这种方式使用标准数组。

回答by Brént Russ?ll

No, there are no dictionaries in php. The closest thing you have is an array. However, an array is different than a dictionary in that arrays have both an index and a key. Dictionaries only have keys and no index. What do I mean by that?

不,php 中没有字典。你拥有的最接近的东西是一个数组。但是,数组与字典的不同之处在于数组同时具有索引和键。字典只有键,没有索引。我是什么意思?

$array = array(
    "foo" => "bar",
    "bar" => "foo"
);

// as of PHP 5.4
$array = [
    "foo" => "bar",
    "bar" => "foo",
];

The following line is allowed with the above array but would give an error if it was a dictionary.

上面的数组允许使用以下行,但如果它是字典,则会出现错误。

print $array[0]

Python has both arrays and dictionaries. Maybe you come from a python background

Python 有数组和字典。也许你来自 python 背景