Home > Backend Development > PHP Problem > How to define an array in php

How to define an array in php

藏色散人
Release: 2023-02-25 19:10:01
Original
6162 people have browsed it

How to define an array in php

How to define an array in php?

How to define arrays in PHP:

1. PHP defines the format of arrays:

array name=array();

For example: $aa=array();//This defines an array,

then assigns a value to the element:

$aa[0]="9016";
$aa[1]="9017";
$aa[2]="9018";
Copy after login

2. How to output an array in PHP:

foreach($aa as $val)
{
echo$val;
}
Copy after login

You can also assign values ​​directly when defining the array

$aa=array(0=>"9016",1=>"9017";2=>"9018");
Copy after login

3. PHP arrays can also be subscripted with characters, not necessarily numbers:

$aa["name"]="Joan";
$aa["num"]="9018";
$aa["email"]="abc@abc.com";
Copy after login

can also be done like this

$aa=array("name"=>"joan","num"=>"9018","email"=>"abc@abc.com");
Copy after login

Define the elements of a one-dimensional array as an array, which is a two-dimensional array.

$aa=array(0=>"a1",1=>"a2");
$bb=array(0=>"b1",1=>"b2");
$cc=array(0=>$aa;1=>$bb);
Copy after login

At this time, $cc[0] is also an array, and $cc[1] is also an array. Array, $cc is a two-dimensional array.

Similarly, three-dimensional and four-dimensional arrays can also continue to be defined.

4. The elements of the array are not only numbers and strings, but also objects of classes.

For more PHP related knowledge, please visit PHP Chinese website!

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

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