Home >Backend Development >PHP Problem >What are the methods to define arrays in php?

What are the methods to define arrays in php?

coldplay.xixi
coldplay.xixiOriginal
2020-07-27 15:30:423369browse

The methods to define arrays in PHP are: 1. PHP defines the format of the array, the code is [array name=array()]; 2. PHP output array method, the code is [foreach($aa as $val) {echo$val;}].

What are the methods to define arrays in php?

The methods to define arrays in PHP are:

1. The format of PHP array definition:

数组名=array();

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

then assigns values ​​to the elements:

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

2, PHP Method of outputting an array:

foreach($aa as $val)
 
{
 
echo$val;
 
}

You can also directly assign a value when defining the array

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

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

$aa["name"]="Joan";
 
$aa["num"]="9018";
 
$aa["email"]=<a href="mailto:abc@abc.com" rel="external nofollow">abc@abc.com</a>;

You can also define the elements of a one-dimensional array as an array, which is a two-dimensional array.

$aa=array("name"=>"joan","num"=>"9018","email"=><a href="mailto:abc@abc.com" rel="external nofollow">abc@abc.com</a>);

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

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

Related learning recommendations:
PHP programming from entry to proficiency

The above is the detailed content of What are the methods to define arrays 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