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
立即学习“PHP免费学习笔记(深入)”;
Multidimensional arrays − It is an array that have one or more arrays
// array with numeric index i.e. Indexed arrays array(value1,value2...); // array with named keys i.e. associative arrays array(key1 => value1, key2 => value2...)
value − Set the value.
key − Set the key.
The array() function returns an array of the parameters.
The following is an example of indexed arrays.
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.
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
以上就是PHP中的array()函数的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号