array() function in PHP
Aug 20, 2023 pm 07:49 PMThe 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
The above is the detailed content of array() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Array functions in PHP8: efficient use of array_pad()

Array functions in PHP8: multiple uses of array_unique()

Quickly extract JSON data from PHP arrays

Detailed explanation of how to use array_diff_key() of PHP array function

How to use Go language array function to sum and return the result?

Array functions in PHP8: various operating techniques of array_slice()

PHP8.1 update: performance improvements for array and string functions

Array functions in PHP8: Examples of various operations of array_intersect_assoc()
