Step-by-Step learning PHP (8) PHP array _php Tutorial

Source: Internet
Author: User
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 (" "); } Echo ('
$city $intro
');
?>


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 ...

  • Related Article

    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.