How to use php array function

青灯夜游
Release: 2023-02-22 21:28:01
Original
3063 people have browsed it

The array() function is a built-in function in PHP, used to create arrays (index arrays, associative arrays, multidimensional arrays). This function creates an array with a key and a value. 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, use => to separate keys and values.

How to use php array function

How to use the php array() function?

php array() function generates an array.

In PHP, there are three types of arrays:

○ Numeric array - an array with a numeric ID key

○ Associative array - an array with a specified key , each key is associated with a value

○ Multidimensional array - an array containing one or more arrays

Syntax

Syntax for indexing arrays:

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

Syntax for associative arrays:

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

Syntax for multi-dimensional arrays:

array( array( value11, value12, ...)
    array( value21, value22, ...)
       ... )
Copy after login

Parameters:

● key: Specifies the key name (numeric value or string).

● Value: Specifies the key value.

Return value: Returns an array with parameters.

Description: array() creates an array with keys and values. If key is omitted when specifying an 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
header("content-type:text/html;charset=utf-8"); 
//创建一个索引数组
$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

The above is the detailed content of How to use php 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