Home  >  Article  >  Backend Development  >  How to define an array in php

How to define an array in php

藏色散人
藏色散人Original
2019-10-02 13:41:536046browse

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";

2. How to output an array in PHP:

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

You can also assign values ​​directly 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"]="abc@abc.com";

can also be done like this

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

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);

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!

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