卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

PHP中的array()函数

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

// 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

<?php
$products = array("Electronics","Clothing","Accessories", "Footwear");
$len = count($products);
for($i = 0;$i<$len;$i++) {
   echo $products[$i];
   echo "<br>";
}
?>

输出

Electronics
Clothing
Accessories
Footwear

The following is an example of associative arrays.

Example

Live Demo

<?php
$rank = array("Football"=>"1","Cricket"=>"2");
foreach($rank as $mykey=>$myvalue) {
   echo "Key = " . $mykey . ", Value = " . $myvalue;
   echo "<br>";
}
?>

输出

Key = Football, Value = 1
Key = Cricket, Value = 2

卓越飞翔博客
上一篇: 如何解决golang报错:invalid memory address or nil pointer dereference
下一篇: PHP sqrt() 函数
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏