Home  >  Article  >  Backend Development  >  How to use array() function in php

How to use array() function in php

王林
王林Original
2020-08-29 15:31:453448browse

How to use the array() function in php: The array() function is used to create an array and return it. For example, if we want to create a numerical array, the syntax is: [array(value1)]. If we want to create an associative array, the syntax is: [array(key=>value)].

How to use array() function in php

array() function is used to create an array and return the created array.

(Recommended tutorial: php video tutorial)

Syntax of numerical array:

array(value1,value2,value3,etc.);

Syntax of associative array:

array(key=>value,key=>value,key=>value,etc.);

Parameters:

  • #key specifies the key name (numeric value or string).

  • #value Specifies the key value.

(Related recommendations: php training)

Example:

Create an associative array named $age:

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age[&#39;Peter&#39;] . " years old.";
?>

Create multidimensional array:

<?php
// 一个二维数组
$cars=array
(
array("Volvo",100,96),
array("BMW",60,59),
array("Toyota",110,100)
);
?>

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

Statement:
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