温馨提示×

php怎么关联数组多个值

PHP
小亿
106
2023-10-18 13:34:06
栏目: 编程语言

在PHP中,可以使用以下两种方式来关联数组的多个值:

  1. 使用索引数组:可以使用数字作为数组的键,将多个值存储在数组中。例如:
$person = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);

可以使用$person["name"]来访问数组中的值。

  1. 使用关联数组:可以使用字符串作为数组的键,将多个值存储在数组中。例如:
$person = array();
$person["name"] = "John";
$person["age"] = 30;
$person["city"] = "New York";

同样可以使用$person["name"]来访问数组中的值。

在这两种方式中,可以使用foreach循环来遍历关联数组的所有键和值:

foreach($person as $key => $value) {
echo $key . ": " . $value . "
";
}

这将输出:

name: John
age: 30
city: New York

0