1. Arrays in PHP
Instead of thinking about arrays in PHP as "arrays" in our narrow sense, I think we might as well put this array in Split, accesses than either as our regular array, accesses than either for our dictionary.
2. Creating an array
If the array does not exist, then storing the values in the array will create an array.
Copy CodeThe code is as follows:
$address [0]= ' Beijing ';
$address [1]= ' Shanghai ';
$address [2]= ' Nanjing ';
$introduce [' Beijing ']= ' capital ';
$introduce [' Shanghai ']= ' International Metropolis ';
$introduce [' Nanjing ']= ' Don't know ';
?>
There is also a more orthodox approach, using the array () language structure, which is also a way of my inclination:
Copy CodeThe code is as follows:
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' beijing ' = ' capital ',
' Shanghai ' = ' International Metropolis ',
' Nanjing ' = ' Don't understand '
);
?>
Of course we can also create an empty array in this way:
Copy CodeThe code is as follows:
$nullArray =array ();
?>
3. Accessing array elements
Accessing array elements is actually the same as in traditional ways:
Copy CodeThe code is as follows:
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' beijing ' = ' capital ',
' Shanghai ' = ' International Metropolis ',
' Nanjing ' = ' Don't understand '
);
Echo ($address [1]);
Echo ($introduce [' Shanghai ']);
?>
4. Iterating over array elements
The most common way to iterate through an array is foreach, which is also more generic.
Copy CodeThe code is as follows:
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' beijing ' = ' capital ',
' Shanghai ' = ' International Metropolis ',
' Nanjing ' = ' Don't understand '
);
foreach ($address as $value)
{
Echo ($value. '
');
}
foreach ($introduce as $key = $value)
{
Echo ("$key = $value
");
}
?>
The foreach traversal of an array is easy, but he has a drawback that he does not manipulate the original array directly, but instead copies a copy of the original array before traversing, which creates a waste of time and space.
Then there is an easy way to do that for.
Copy CodeThe code is as follows:
$address =array (' Beijing ', ' Shanghai ', ' Nanjing ');
$introduce =array (' beijing ' = ' capital ',
' Shanghai ' = ' International Metropolis ',
' Nanjing ' = ' Don't understand '
);
for ($i =0; $i {
Echo ("$address [$i]
");
}
?>
Although this is simple, there are drawbacks, that is, you can only iterate through the index array, there is no way to traverse the dictionary.
As a result, an iterator function is proposed in PHP.
The most common of these is the each () function. Let's look at a simple example:
Copy CodeThe code is as follows:
$introduce =array (' city name ' = ' Introduction ',
' Beijing ' = ' capital ',
' Shanghai ' = ' International Metropolis ',
' Nanjing ' = ' Don't understand '
);
Reset ($introduce);
Echo ('
'); while (list ($city, $intro) =each ($introduce)) {echo ("
$city |
$intro | "); } Echo ('
');
?>
As an explanation, each () function iterates over the array elements, similar to our iterator in the general sense. And the biggest benefit of using an iterative function is that it does not produce a copy of the original array like the Foreach language structure, which is useful when working with large arrays.
http://www.bkjia.com/PHPjc/321375.html www.bkjia.com true http://www.bkjia.com/PHPjc/321375.html techarticle 1. The array in PHP and the array in PHP to understand it as our narrow "array", I think it may also be useful to divide this array, accesses than either for our regular array, accesses than either for us ...