What is an array? An array is a programming structure. It is a variable that stores a group or a series of values.
For example, personal identity registration during census, such as name, gender, ethnicity, and birth, can be used as an array.
The array created in the php Tutorial is defined using the array () structure, for example:
$ People = array ('name', 'sex', 'nation', 'brith ');
To display the values of each element in an array, we use an index starting from 0. The index number is in square brackets after the variable name. For example:
<? Php
$ People = array ('name', 'sex', 'nation', 'birth ');
Echo $ people [2];
?>
The output $ people [2] indicates that nation is displayed (the first index item is counted from 0 ).
In addition to numeric index arrays, php also supports related arrays. Related arrays are used to replace unintuitive numeric indexes with custom keywords. For example:
<? Php
$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
Echo $ doneles ['CS '];
?>
The related array is used to make the output selection intuitive (no pre-calculation of index numbers is required and then output). The defined keywords and values are defined using the "=>" symbol.
Based on the two display methods of php array elements, you can also create numbers automatically without the need for array () declaration and initialization like variables. For example
$ People [0] = 'name ';
$ People [1] = 'sex ';
$ People [2] = 'Nation ';
$ People [3] = 'brith ';
Or
$ Les ['xm '] = 'name ';
$ Doneles ['xb '] = 'sex ';
$ Doneles ['mz'] = 'Nation ';
$ Les ['CS '] = 'birth ';
The size of the array varies dynamically according to the number of added elements.
2. Display of array elements
In the above example, no matter $ people [2] or $ minutes les ['CS '], only an array element value with a known clear position is output, how to quickly output all or part of the array elements is undoubtedly the fastest way to use loop statements.
<? Php
$ People = array ('name', 'sex', 'nation', 'birth ');
For ($ I = 0; $ I <4; $ I ++)
Echo "$ people [$ I]";
?>
In addition to the for loop that understands the number of loops, you can also use a foreach statement that does not require the number of loops.
<? Php
$ People = array ('name', 'sex', 'nation', 'birth ');
Foreach ($ people as $ xiangmu)
Echo $ xiangmu;
?>
The $ xiangmu variable saves the element values in the array and displays them in sequence. Of course, in order to differentiate the output data at intervals, spaces can be output after the array elements:
Echo $ xiangmu ."";
Now let's look at a php file that filters out repeated data from the array.
<? Php
$ Num = count ($ array );
If ($ num)
{
Sort ($ array );
}
If ($ num! = 0)
{
$ M = $ array [0];
$ N = 0;
$ Kind = 1;
Echo $ array [0]. "----------";
For ($ z = 0; $ z <$ num; $ z ++)
{
If ($ m! = $ Array [$ z])
{
Echo $ array [$ Z-1]. "";
Echo $ array [$ z];
$ Kind = 0;
$ M = $ array [$ z];
}
$ N ++;
$ M ++;
// Echo $ array [$ z]. "'/n '";
}
If ($ kind = 1)
{
Echo $ array [$ num-1]. "duplicate data! ";
}
?>
Related array functions
List ()
Array_walk
Allows the user-defined function to process every element in the array.
Syntax: int array_walk (array arr, string func );
Return value: integer
Refer to each () list ()
Arsort
Sorts the values of an array from large to small.
Syntax: void arsort (array );
Refer to asort () rsort () ksort () sort ()
Asort
Sorts the values of an array from small to large.
Syntax: void asort (array );
Return value: None
See arsort () rsort () ksort () sort ()
Count
Calculates the number of elements in a variable or array.
Syntax: int count (mixed var );
Return value: integer
Refer to end () next () prev () reset ()
Each
Returns the index and value of the next element in the array.
Syntax: array each (array );
Returned value: Array