php 关联数组 索引数组
In an associative array, we can specify the values with the keys. Then, following two things are required to create an associative array: 1) key, and 2) value
在关联数组中 ,我们可以使用键指定值。 然后,需要执行以下两项操作来创建关联数组:1) key和2) value
key can be used as an index to access the value from the array.
键可以用作从数组访问值的索引。
Note: To create an associative or other types of array, we use array() function.
注意:要创建关联数组或其他类型的数组,请使用array()函数 。
Syntax:
句法:
array(key1=>value1, key2=>value2, key3=>value3, ...);
Example:
例:
In this example, we are creating an associative array, printing the values: 1) Using keys, and 2) Printing complete array with keys & values
在此示例中,我们将创建一个关联数组,打印值:1)使用键,以及2)打印包含键和值的完整数组
<?php
//creating student array with keys & values
$std = array('id' => "101", 'name' => "Amit", 'course' => "B.Tech");
//printing elemenets
print ("std elements...\n");
print ("Id = " . $std['id'] . " Name = " . $std['name'] . " Course = " . $std['course']);
//printing array using for each loop
//printing complete array
print ("std...\n");
print_r($std);
?>
Output
输出量
std elements...
Id = 101 Name = Amit Course = B.Techstd...
Array
(
[id] => 101
[name] => Amit
[course] => B.Tech
)
翻译自: https://www.includehelp.com/php/create-an-associative-array.aspx
php 关联数组 索引数组