1. arrays in PHP
Instead of interpreting the array in PHP as an array in our narrow sense, I think we may also split this array into two parts. One is our regular array, and the other is our dictionary.
2. Create an array
If the array does not exist, an array is created when values are stored in the array.
<? PHP
$ Address[0] ='Beijing';
$ Address[1] ='Shanghai';
$ Address[2] ='Nanjing';
$ Introduce['Beijing'] ='Capital';
$ Introduce['Shanghai'] ='International Metropolis';
$ Introduce['Nanjing'] ='I don't know';
?>
There is also a more orthodox method that uses the Array () language structure, which is also one of my preferences:
<? PHP
$ Address=Array('Beijing','Shanghai','Nanjing');
$ Introduce=Array('Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>'I don't know'
);
?>
Of course, we can also create an empty array in this way:
<? PHP
$ Nullarray=Array();
?>
3. Access array elements
Accessing array elements is actually the same as traditional methods:
<? PHP
$ Address=Array('Beijing','Shanghai','Nanjing');
$ Introduce=Array('Beijing'=>'Capital',
'Shanghai'=>'International Metropolis',
'Nanjing'=>'I don't know'
);
Echo($ Address[1]);
Echo($ Introduce['Shanghai']);
?>
4. Traverse array elements
The most common way to traverse arrays is foreach.
<? PHP
$ Address = Array ( 'Beijing' , 'Shanghai' , 'Nanjing' );
$ Introduce = Array ( 'Beijing' => 'Capital' ,
'Shanghai' => 'International Metropolis' ,
'Nanjing' => 'I don't know'
);
Foreach ( $ Address As $ Value )
{
Echo ( $ Value . '<Br/>' );
}
Foreach ( $ Introduce As $ Key => $ Value )
{
Echo ( " $ Key => $ Value <Br/>" );
}
?>
Foreach is easy to traverse the array, but he does not directly operate the original array, but copies a copy of the original array before traversing, this results in a waste of time and space.
A simple method is.
<? PHP
$ Address = Array ( 'Beijing' , 'Shanghai' , 'Nanjing' );
$ Introduce = Array ( 'Beijing' => 'Capital' ,
'Shanghai' => 'International Metropolis' ,
'Nanjing' => 'I don't know'
);
For ( $ I = 0 ; $ I <Count ( $ Address ); $ I ++)
{
Echo ( " $ Address [ $ I ] <Br/>" );
}
?>
This is simple, but it also has the disadvantage that it can only traverse the index array and there is no way to traverse the dictionary.
Therefore, an iterator function is proposed in PHP.
The most common function is the each () function. Let's look at a simple example:
<? PHP
$ Introduce = Array ( 'City name' => 'Introduction' ,
'Beijing' => 'Capital' ,
'Shanghai' => 'International Metropolis' ,
'Nanjing' => 'I don't know'
);
Reset ( $ Introduce );
Echo ( '<Table>' );
While ( List ( $ City , $ Intro ) = Each ( $ Introduce ))
{
Echo ( "<Tr> <TD> $ City </TD> <TD> $ Intro </TD>" );
}
Echo ( '</Table>' );
?>
The each () function is used to traverse array elements, similar to our iterator in the general sense. In addition, the biggest advantage of using iterative functions is that they do not generate a copy of the original array like the foreach structure, which is useful in processing large arrays.