1. The array in PHP
Instead of understanding the arrays in PHP as "arrays" in our narrow sense, I think we might as well divide the array into one, one as our regular array, one our dictionary.
2. Create an array
If the array does not exist, then storing the value in the array creates an array.
Copy Code code as follows:
<?php
$address [0]= ' Beijing ';
$address [1]= ' Shanghai ';
$address [2]= ' Nanjing ';
$introduce [' Beijing ']= ' capital ';
$introduce [' Shanghai ']= ' international Metropolis '];
$introduce [' Nanjing ']= ' don't understand ';
?>
There is also a more orthodox way to use the array () language structure, which is one of my tendencies:
Copy Code code as follows:
<?php
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' capital of Beijing ' => '),
' Shanghai ' => ' International Metropolis ',
' Nanjing ' => ' don't understand '
);
?>
Of course, we can also create an empty array in this way:
Copy Code code as follows:
<?php
$nullArray =array ();
?>
3. Accessing array elements
Accessing array elements is actually the same as the traditional approach:
Copy Code code as follows:
<?php
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' capital of Beijing ' => '),
' Shanghai ' => ' International Metropolis ',
' Nanjing ' => ' don't understand '
);
Echo ($address [1]);
Echo ($introduce [' Shanghai ']);
?>
4. Traversing array elements
The most common way to traverse an array is foreach, which is also more generic.
Copy Code code as follows:
<?php
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' capital of Beijing ' => '),
' Shanghai ' => ' International Metropolis ',
' Nanjing ' => ' don't understand '
);
foreach ($address as $value)
{
Echo ($value. ' <br/> ');
}
foreach ($introduce as $key => $value)
{
Echo ("$key => $value <br/>");
}
?>
foreach is easy to traverse an array, but he has a disadvantage that he does not directly manipulate the original array, but before traversing a copy of the original array, which creates a waste of time and space.
So there's an easy way, for.
Copy Code code as follows:
<?php
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' capital of Beijing ' => '),
' Shanghai ' => ' International Metropolis ',
' Nanjing ' => ' don't understand '
);
for ($i =0; $i <count ($address); $i + +)
{
Echo ("$address [$i]<br/>");
}
?>
This is simple, but there are drawbacks, that is, can only traverse the index array, there is no way to traverse the dictionary.
Thus, an iterator function is proposed in PHP.
The most common one is the each () function. Let's look at a simple example:
Copy Code code as follows:
<?php
$introduce =array (' City name ' => ' Introduction ',
' Beijing ' => ' capital ',
' Shanghai ' => ' International Metropolis ',
' Nanjing ' => ' don't understand '
);
Reset ($introduce);
Echo (' <table> ');
while (list ($city, $intro) =each ($introduce))
{
Echo ("<tr><td> $city </td><td> $intro </td>");
}
Echo (' </table> ');
?>
To explain, the each () function is used to traverse an array element, similar to our iterator in the general sense. And the great advantage of using an iterative function is that it does not produce a copy of the original array like a foreach language structure, which is useful when dealing with large arrays.