PHP中的array()函数

wufei123 发布于 2023-08-21 阅读(1111)

PHP中的array()函数

The array() function in PHP creates an array.

Array is of three types in PHP.

  • Indexed arrays − It is an array with numeric index

  • Associative arrays − It is an array with named keys

  • Multidimensional arrays − It is an array that have one or more arrays

Syntax

1

2

3

4

// array with numeric index i.e. Indexed arrays

array(value1,value2...);

// array with named keys i.e. associative arrays

array(key1 => value1, key2 => value2...)

Parameters

  • value − Set the value.

  • key − Set the key.

Return

The array() function returns an array of the parameters.

The following is an example of indexed arrays.

Example

Live Demo

1

2

3

4

5

6

7

8

$products = array("Electronics","Clothing","Accessories", "Footwear");

$len = count($products);

for($i = 0;$i<$len;$i++) {

   echo $products[$i];

   echo "
"
;

}

?>

输出

1

2

3

4

Electronics

Clothing

Accessories

Footwear

The following is an example of associative arrays.

Example

Live Demo

1

2

3

4

5

6

7

$rank = array("Football"=>"1","Cricket"=>"2");

foreach($rank as $mykey=>$myvalue) {

   echo "Key = " . $mykey . ", Value = " . $myvalue;

   echo "
"
;

}

?>

输出

1

2

Key = Football, Value = 1

Key = Cricket, Value = 2


以上就是PHP中的array()函数的详细内容,更多请关注php中文网其它相关文章!


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。