Home >Backend Development >PHP Tutorial >PHP learning officially sets sail (3)

PHP learning officially sets sail (3)

黄舟
黄舟Original
2016-12-28 09:12:34965browse

Now talk about arrays
There are 3 types of php arrays

Numeric array Array with numeric ID key
Associative array Each ID key in the array is associated with a value
Multidimensional array Array containing one or more arrays

Declaration about arrays

<?php
$a[5];
$a[5]={1,2,3,4,5};
?>

Conventional 2 types like C language will not work in PHP

php has the keyword array which is used to define arrays

<?php
$a=array();
?>

This defines an empty array. There is no need to specify the length. The elements inside can be added dynamically. How many are added? This The array is as big as it is, and you can continue to add it. This is very cool.

<?php
$a=array();
echo $a;
?>

This way you can print the type of a and the result is Array

But in this case, an error will be reported

<?php
$a=array();
echo $a[0];
?>

Because the array is empty

There are two main ways to define an array. One is to use the array

<?php
$a=array(1,2,3,4,5);
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>

count keyword to calculate how many there are in an array. elements

Another option is to

<?php
$a[0]=&#39;a&#39;;
$a[1]=&#39;b&#39;;
$a[2]=&#39;c&#39;;
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>

directly assign a value to the variable subscript, and the variable will automatically become an array

But the values ​​must be assigned in subscript order
For example, this is wrong

<?php
$a[0]=&#39;a&#39;;
$a[1]=&#39;b&#39;;
$a[2]=&#39;c&#39;;
$a[5]=&#39;d&#39;;
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>

Also the elements in the array can not be of the same type

<?php
$a=array(1,&#39;b&#39;,"hello",1.0);
for($i=0;$i<count($a);$i++)
echo $a[$i]."<br/>";
?>

Do you think it is very powerful


Now let’s talk about associative arrays
Associative arrays are more powerful than ordinary arrays. The subscripts do not need to use numbers, but choose their own names.
This is a key-to-value relationship, similar to java’s map* *Very similar

<?php
$a=array("a"=>1,&#39;b&#39;=>2,"c"=>3);
echo $a["a"]."<br/>";
echo $a[&#39;b&#39;]."<br/>";
echo $a["c"]."<br/>";
?>

Note that it is=>not->, the single quotes and double quotes inside can be interchanged
Key=>The value key can be repeated, but the result is to display the last one

<?php
$a=array("a"=>1,&#39;a&#39;=>2,"c"=>3);
echo $a["a"]."<br/>";
echo $a["c"]."<br/>";
?>

Print 2 3

There is also a definition of associative array, which is the one mentioned above

<?php
$a["a"]="hello";
$a["b"]="world";
echo $a["a"]."<br/>";
echo $a["b"]."<br/>";
?>

But the following is wrong

<?php
$a["a"]=>"hello";
$a["b"]=>"world";
echo $a["a"]."<br/>";
echo $a["b"]."<br/>";
?>

In addition, numbers can also be used as keys

<?php
$a["1"]="hello";
$a["2"]="world";
echo $a["1"]."<br/>";
echo $a["2"]."<br/>";
?>

is feasible

It can be output without quotation marks, but php has a prompt to note, do not use this

<?php
$a[&#39;a&#39;]="hello";
$a[&#39;b&#39;]="world";
echo $a[a]."<br/>";
echo $a [ b ]";
?>

Let’s talk about multi-dimensional arrays
In multi-dimensional arrays, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on

This defines a multi-dimensional array, two-dimensional

<?php
$a=array(array(&#39;a&#39;,1,2),array("hello",3,1.1,));
echo $a[0][0];
?>

In C language, it is a [2][3] It doesn’t matter how many dimensions the array has.
Similarly, the elements in a multi-dimensional array can also be of multiple types
And it can also be like this

<?php
$a=array(array(&#39;a&#39;,1,2),array("hello",3,1.1,2,&#39;a&#39;));
echo $a[0][0];
?>

Not required The number of elements in each sub-array is the same, which is better than C language.

Like ordinary arrays, multi-dimensional arrays can also be defined in this way, but I don’t think anyone will do this. .

<?php
$a[0][0]="hello00";
$a[0][1]="hello01";
$a[0][2]="hello02";
$a[0][3]="hello03";
$a[1][0]="hello10";
$a[1][1]="hello11";
$a[1][2]="hello12";
$a[1][3]="hello13"; 
for($i=0;$i<2;$i++){
for($j=0;$j<4;$j++)
echo $a[$i][$j]." "; echo "<br/>";
}
?>

This two-dimensional array is a[2][4] with 2 rows and 4 columns. It is relatively regular.
Note that only the number of elements in the columns can be printed using a loop. In C language, You don’t need to consider this sentence

You can also define a multi-dimensional associative array

<?php
$a=array(&#39;a&#39;=>array(&#39;a&#39;=>"hello",&#39;b&#39;=>"world"),&#39;b&#39;=>array(&#39;one&#39;=>1,&#39;two&#39;=>2,&#39;three&#39;=>3)); 
echo $a[&#39;a&#39;][&#39;a&#39;]; 
?>

will print hello
, which looks a bit dizzy, because the associative array contains The associated
does not need to be like this. Like the following, it will be clear that many

<?php
$a=array(&#39;a&#39;=>array("hello","world"),&#39;b&#39;=>array(1,2,3)); 
echo $a[&#39;a&#39;][0]."<br/>".$a[&#39;b&#39;][2]; 
?>

print out

hello
3

must not be played like this

<?php
$a=array(array(&#39;a&#39;=>"hello",&#39;b&#39;=>"world"),array(&#39;one&#39;=>1,&#39;two&#39;=>2,&#39;three&#39;=>3)); echo $a[&#39;a&#39;]; ?>

Wrong



What I mentioned above are all two-dimensional arrays, so how to define three-dimensional or above arrays is very simple

<?php
$a=array(array(array(1,2,3))); 
echo $a[0][0][0]; 
?>

Print 1

<?php
$a=array(array(array(1,2,3)),array(array(4,5,6))); 
echo $a[1][0][0]; 
?>

Print 4

<?php
$a=array(array(array(1,2,3),array(4,5,6)),array(array(7,8,9))); 
echo $a[0][1][1]; 
?>

Print 5

Someone should be dizzy watching it
Now analyze
For example, $a[0][1][1]; The element in the rightmost square bracket represents the innermost element in the array
$a=array(array(array(1,2,3),array (4,5,6)),array(array(7,8,9)));
is divided into up to 3 levels. The element in the rightmost bracket represents the innermost layer
and then to the left The square brackets are moved to the outer layer, and so on

In fact, you will understand if you look at it more. The several layers are divided into several dimensional arrays
In addition, you don’t need to understand the 3-dimensional array too thoroughly. Generally, you can master it. Two-dimensional is enough

I didn’t mention the foreach loop in detail before. In fact, it’s best to use the foreach loop to traverse a one-dimensional array

<?php
$a=array(1,"hello",&#39;a&#39;); 
foreach($a as $value)
echo $value."<br/>"; 
?>

Output

1
hello
a

Isn’t it very simple?
$value is just a temporary variable, used to save array elements. You can call it whatever you want
It is equivalent to giving an array to the proxy variable and letting it help output


<?php
$a=array(1,"hello",&#39;a&#39;); foreach($a as $value)
echo $a."<br/>"; ?>

This will not output the array elements
Only output

Array
Array
Array

The above is the content of PHP Learning Official Set sail (3), more For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

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