Step by step learning PHP (8) php array _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Learn the PHP (8) php array step by step. 1. arrays in PHP should be interpreted as arrays in PHP in our narrow sense. I think we may also split this array into two parts. one is our regular array, and the other is ours. 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.

The 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 method that uses the array () language structure, which is also one of my preferences:

The code is as follows:


$ Address = array ('Beijing', 'Shanghai', 'Nanjing ');
$ Introduce = array ('Beijing' => 'capital ',
'Shanghai' => 'international Metropolis ',
'Nanjing '=>' don't know'
);
?>


Of course, we can also create an empty array in this way:

The code is as follows:


$ NullArray = array ();
?>


3. access array elements
Accessing array elements is actually the same as traditional methods:

The code is as follows:


$ Address = array ('Beijing', 'Shanghai', 'Nanjing ');
$ Introduce = array ('Beijing' => 'capital ',
'Shanghai' => 'international Metropolis ',
'Nanjing '=>' don't know'
);
Echo ($ address [1]);
Echo ($ introduce ['Shanghai']);
?>


4. Traverse array elements
The most common way to traverse arrays is foreach.

The code is as follows:


$ Address = array ('Beijing', 'Shanghai', 'Nanjing ');
$ Introduce = array ('Beijing' => 'capital ',
'Shanghai' => 'international Metropolis ',
'Nanjing '=>' don't know'
);
Foreach ($ address as $ value)
{
Echo ($ value .'
');
}
Foreach ($ introduce as $ key => $ value)
{
Echo ("$ key => $ value
");
}
?>


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.

The code is as follows:


$ Address = array ('Beijing', 'Shanghai', 'Nanjing ');
$ Introduce = array ('Beijing' => 'capital ',
'Shanghai' => 'international Metropolis ',
'Nanjing '=>' don't know'
);
For ($ I = 0; $ I {
Echo ("$ address [$ I]
");
}
?>


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:

The code is as follows:


$ Introduce = array ('City name' => 'Introduction ',
'Beijing' => 'capital ',
'Shanghai' => 'international Metropolis ',
'Nanjing '=>' don't know'
);
Reset ($ introduce );
Echo ('






');While (list ($ city, $ intro) = each ($ introduce )){Echo (" ");}Echo ('
$ City $ Intro
');
?>



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.

Http://www.bkjia.com/PHPjc/321375.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/321375.htmlTechArticle1. the arrays in PHP, instead of the arrays in PHP, are our narrow "arrays". I think we may also split this array into two parts. one is our regular array, one 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.