Step by Step Learning PHP (8) PHP array _php base

Source: Internet
Author: User
Tags arrays
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.