Home > php教程 > PHP开发 > body text

Definition, initialization and display implementation code of array elements in PHP

高洛峰
Release: 2016-12-26 13:46:18
Original
1316 people have browsed it

From ASP to PHP for the first time, I feel that one of the strengths of PHP is the richness of built-in functions. For example, the PHP date and time functions I learned earlier, related functions for reading and writing files, etc. all show that PHP is more professional and useful. It is easy for users to use.

At first I was very excited about the rich functions of PHP functions. As I came into contact with more and more almost abnormal functions, I suddenly thought of the scarcity of ASP built-in functions. To complete a certain special function, it is often necessary to It is necessary to customize functions. As the number of applications increases, I actually have a set of commonly used function libraries. However, now in PHP, these functions have long been standardized, standardized and condensed into built-in functions for direct use. Former ASP developers have become ordinary users of PHP.
But on second thought, the existence of these functions and these large numbers of PHP functions at least shows that PHP is more professional; at the same time, it should be very fast and easy to use when processing our daily PHP programs, which makes developers no longer Customize functions for some basic functions and detailed functions, and focus your main energy on building more powerful program modules. Therefore, I have strengthened my belief in taking a look at PHP functions, but I think that in the future development process, the PHP function manual should be a portable book.

Of course, there is no need to discuss the debate about the advantages and disadvantages of ASP and PHP. Learning and understanding can help you understand the truth.

To get back to the point, there are too many PHP functions to prevent forgetting, so every time I read a type of function, I will summarize and collect it, and write a log for convenience.

1, Definition and initialization of arrays

What is an array? An array is a programming structure that is a variable that stores a set or series of values.
For example, the identity registration of individuals during the census, such as name, gender, ethnicity, birth, etc., can be used as an array.
Arrays created in PHP are defined using the array() structure, for example:

$people=array('name','sex','nation','brith');

How to display the value of each element in the array, we use the index starting from 0, the index number is in square brackets after the variable name, for example:

<?php
$people=array(&#39;name&#39;,&#39;sex&#39;,&#39;nation&#39;,&#39;birth&#39;);
echo $people[2];
?>
Copy after login

The $people[2] output shows nation (the first item of the index is counted from 0).

PHP supports related arrays in addition to numerically indexed arrays. The so-called related array means that you can customize keywords to replace unintuitive numeric indexes, such as:

<?php
$peoples=array(&#39;xm&#39;=>&#39;name&#39;,&#39;xb&#39;=>&#39;sex&#39;,&#39;mz&#39;=>&#39;nation&#39;,&#39;cs&#39;=>&#39;birth&#39;);
echo $peoples[&#39;cs&#39;];
?>
Copy after login

Using related arrays makes the output selection intuitive (not The index number needs to be pre-calculated and then output), and the "=>" symbol is used between the defined keyword and value.

According to the two display methods of PHP array elements, numbers can also be automatically created directly without array() declaration and initialization like variables. For example,

$people[0]='name';
$people[1]='sex';
$people[2]='nation';
$people[3 ]='brith';

or

$peoples['xm']='name';
$peoples['xb']='sex';
$ peoples['mz']='nation';
$peoples['cs']='birth';

The size of this array changes dynamically according to the number of added elements.

2, Display of array elements

No matter $people[2] or $peoples['cs'] used above, they only output the known and clear position. How to quickly output all or part of the array element values ​​using a loop statement is undoubtedly the fastest way.

<?php
$people=array(&#39;name&#39;,&#39;sex&#39;,&#39;nation&#39;,&#39;birth&#39;);
for ($i=0;$i<4;$i++)
  echo "$people[$i] ";
?>
Copy after login

In addition to using a for loop that knows the number of loops, you can also use a foreach statement that does not require the number of loops.

<?php
$people=array(&#39;name&#39;,&#39;sex&#39;,&#39;nation&#39;,&#39;birth&#39;);
foreach($people as $xiangmu)
  echo $xiangmu;
?>
Copy after login

$xiangmu variable will save the values ​​of each element in the array and display them in sequence. Of course, in order to distinguish the output data, a space can be output after the array elements:
echo $xiangmu." ";
Note: English periods (.) can concatenate strings into new strings

For more articles on the definition, initialization and display implementation code of array elements in PHP, please pay attention to the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!