Php learning array courseware page 1/2 _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php learning array courseware 12th page. Subscript: the recognition name in the array, that is, the string or integer, which has several index values in the code array, is called a dimension array. Index value: the index is a column or subscript in the database table: the recognition name in the array, that is, the code of the string or integer in the array.

Several index values in the array are called dimension arrays.
Index value: an index is a structure that sorts the values of one or more columns in a database table.

Array classification
PHP arrays are divided into two types:
Index array: the index value of the index (indexed) is an integer starting with 0. it is used to identify objects by position.
Join array: associative is associated with the index value of the string. The index value is the column name, and the term is used to access the data of the column.

Arrays are usually assigned values.
There are two ways to assign values to an array:
$ A [1] = "dsadsadsa ";
$ B [2] = "dsadsadsad ";
Use the array function:
$ A = array ("dsads", "dsadsa", 321312 );
One-dimensional array: an array is called a one-dimensional array when there is only one index value (subscript.
Format of direct array assignment:
$ Array variable name [index value] = data content;
Note: the index values can be strings or integers, but 1 and 1 are different. one belongs to the integer and the other belongs to the string.

Arrays with the same name without indexed values are arranged in order.
Instance:
$ A = array (1, 2, 3, 4, 5, 6 );
$ B = array ("one", "two", "three ");
$ C = array (0 => "aaa", 1 => "bbb", 2 => "ccc ");
$ D = array ("aaa", 6 => "bbb", "ccc ");
$ E = array ("name" => "zhang", "age" => 20 );
?>
Two-dimensional array
Multi-dimensional array format:
$ A [0] [] = "dsadas ";
$ A [0] [] = "dsadsa"; this group is 1 and 2 of the 0 index values under $
If the format is declared using the array function:
$ A = array ("dsadsa", "dsadas", 21, array ("dsadsa", "dsadas "));

Array traversal
Foreach loop structure:
Foreach only uses two formats: loop and array
Foreach (array_exprssion (array expression) as $ value );
Foreach (array_exprssion (array expression) as $ key => $ value );
The first format traverses the given array_exprssion array. The current value in each loop is assigned to $ calue, and the pointer inside the array moves one step forward.
In the second format, only the key values of the current unit are assigned to the variable $ key in each loop.
When foreach starts to execute, the pointer inside the array automatically points to the first unit. In addition, note that foreach operates on a copy of the specified array, rather than the array itself.
$ Arr = array (10, 20, 30, 40, 50, 60 );
Foreach ($ arr as $ k =>$ v ){
Echo "$ k => $ v
";
}

Output result: 0 => 10 1 => 20 2 => 30 3 => 40 4 => 50 5 => 60 // subscript => integer
Combined use of list (), each (), and while loops
Each ():
$ Arr = array (1, 2, 3, 4, 5 );
$ A = each ($ arr );
Print_r ($ );
Output result: Array ([1] => 1 [value] => 1 [0] => 0 [key] => 0)
Obtains the value subscript key of the first part of the array.
List ():
$ Arr3 = array ("a", "B", "c ");
List ($ key, $ value) = each ($ arr3 );

Echo $ key ."
". $ Value;
Output result: 0 a list () can be used to assign values to a group of variables in one step operation. it is assumed that the numeric index starts from 0.
While loop
$ Arr = array (1, 2, 3, 4, 5, 6, 7, 8, 9 ,);
While (list ($ key, $ value) = each ($ arr )){
$ Key ++;
Echo $ key. "=>". $ value;
Echo"
";
}
Echo"
";
Output result: 1 => 1 2 => 2 3 => 3 4 => 4 5 => 5 6 => 6 7 => 7 8 => 8 9 => 9
Reset () array pointer redirection
After executing each (), the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array.
Is_array: checks whether the variable is an array. true or false is returned.
$ Arr = array (1, 2, 3, 4, 5, 6, "saas ");
While (list ($ k, $ v) = each ($ arr ))
{
If (is_array ($ arr ))
{
$ X + = $ v;
Echo $ x;
}
Else
{
$ X + = $ k;
}
}
This example does not fully reflect the functions of is_array, but can be referenced.
Array pointer
Next (): Moves the pointer backward.
Prve (): Moves the pointer forward
End (): points the pointer to the last element of the array.
Reset (): Moves the current pointer to the first index position unconditionally.
Syntax format: mixed next (array name)
$ Arr = (array (1, 2, 3, 4, 5 ));
Echo end ($ arr );
Output result: 5
Key () and current () and count ()
The key () function reads the index value of the data pointed to by the current pointer.
The current () function reads the content of the data pointed to by the current pointer.
The count () function is used to calculate the number of all elements in the array, that is, the function returns the length value of the target array.
Format: int count (array name );

Key (): get the key name from the joined array
$ Array = array ('fruit1' => 'apple', 'fruit2' => 'Orange ', 'fruit3' => 'grape ', 'fruit4' => 'apple', 'fruit5' => 'apple ');
While ($ fruit_name = current ($ array )){
If ($ fruit_name = 'apple '){
Echo key ($ array ).'
';
}
Next ($ array );
}
Output results: fruit1, fruit4, fruit5

Current (): returns the current unit in the array.
$ Transport = array ('foot', 'Bike', 'car', 'Plane ');
$ Mode = current ($ transport); // $ mode = 'foot ';
$ Mode = next ($ transport); // $ mode = 'bike ';
$ Mode = current ($ transport); // $ mode = 'bike ';
$ Mode = prev ($ transport); // $ mode = 'foot ';
$ Mode = end ($ transport); // $ mode = 'plane ';
$ Mode = current ($ transport); // $ mode = 'plane ';
Note: the current unit in the array is returned by the example.

Count (): calculates the number of units in the array.
$ Arr = array (1, 2, 3, 4, 5, 6 );
Echo count ($ arr );
Output result: 6

Array_change_key_case ()
Array_change_key_case returns an array of string keys in lowercase or uppercase.
Two [CASE_UPPER] Morphological functions are converted to uppercase, and [CAS_LOWER] to lowercase.
$ Input_array = array ("FirSt" => 1, "SecOnd" => 4 );
Print_r (array_change_key_case ($ input_array, CASE_UPPER ));
Output result: Array ([FIRST] => 1 [SECOND] => 4)

Array_chunk ()
The array_chunk () function splits the data content of the target array into several small arrays by specifying the number of indexes.
$ Arr = array (1, 2, 3, 4, 5, 6 );
$ A = array_chunk ($ arr, 3 );
Print_r ($ );
Output result: Array ([0] => Array ([0] => 1 [1] => 2 [2] => 3) [1] => Array ([0] => 4 [1] => 5 [2] => 6 ))
That is, it is equal to dividing by 3 by the sum of the number of array units.

Array_count_values
Array_count_values is used to calculate the number of occurrences of each value in the target array.
Syntax format: array_count_values (target array)
The result value returned by this function uses the content and data of the original array as an index and is represented as an array.
$ Arr = array (1, 2, 3, 2, 6 );
Print_r (array_count_values ($ arr ));
Output result: Array ([1] => 1 [2] => 2 [3] => 2 [6] => 1)

Several index values in the dimensions array are called dimension arrays. Index value: the index is a column or...

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.