array() function in PHP

PHPz
Release: 2023-08-20 19:50:02
forward
1545 people have browsed it

array() function in PHP

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...)
Copy after login

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>";
}
?>
Copy after login

输出

Electronics
Clothing
Accessories
Footwear
Copy after login

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>";
}
?>
Copy after login

输出

Key = Football, Value = 1
Key = Cricket, Value = 2
Copy after login

The above is the detailed content of array() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!