What is an array? An array is a programming structure. it is a variable that stores a group or a series of numerical values. for example, it can be used to register an individual's identity during census, such as name, gender, ethnicity, birth, etc.
What is an array? An array is a programming structure. it is a variable that stores a group or a series of numerical values, such as name, gender, ethnicity, birth, etc. can be used as an array.
The array created in php 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:
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Echo $ people [2];
- ?>
The output $ people [2] indicates that nation (the first index item is counted from 0) is displayed. in addition to the numerical index array, php also supports related arrays, so-called related arrays, A custom keyword can be used to replace an unintuitive Digital Index, for example:
- $ 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, such:
- $ 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 based on the number of elements added.
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:
- $ 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 cycles, you can also use a foreach statement that does not require the number of cycles:
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Foreach ($ people as $ xiangmu)
- Echo $ xiangmu;
- ?>
The $ xiangmu variable will save the values of each element in the array and display them in sequence. of course, to differentiate the output data at intervals, a space can be output after the array element:
Echo $ xiangmu. ""; now let's take a look at a php to filter out repeated data from the array:
- $ 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! ";
- }
- ?>