Home > Backend Development > PHP Tutorial > How to use array function

How to use array function

藏色散人
Release: 2023-04-05 07:00:01
Original
14043 people have browsed it

PHP array() function is used to create arrays.

How to use array function

php array() function syntax

Function: Generate an array

Syntax:

Index array:

array(value1,value2,value3,etc.);
Copy after login

Associative array:

array(key=>value,key=>value,key=>value,etc.);
Copy after login

Parameters:

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

value specifies the key value.

Description: array() creates an array with keys and values. If the key is omitted when specifying the array, an integer key is generated that starts at 0 and increments by 1. To create an associative array with array(), use => to separate the keys and values. To create an empty array, pass no arguments to array().

php array() function example

<?php
//创建一个索引数组
$teacher = array("西门","灭绝","无忌");
$len = count($teacher);
for ($i = 0; $i < $len; $i++)
{    
echo $teacher[$i];    
echo "<br>";
}
echo "<br>";
//创建一个关联数组并输出
$teacher = array("class" => "php中文网","name" => "peter","age" => "30");
echo $teacher[&#39;class&#39;]. "有个老师名字叫" .$teacher[&#39;name&#39;]. ",他有" .$teacher[&#39;age&#39;]. "岁了";
?>
Copy after login

Output:

西门
灭绝
无忌
php中文网有个老师名字叫peter,他有30岁了
Copy after login

This article is an introduction to the PHP array function. I hope it will be helpful to friends who need it. Helps!

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

Related labels:
source:php.cn
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