PHP learning notes array definition
1. no index method
$ List = array ("test", "test1", "test2", "test3 ");
The default index of the array defined by this method starts from 0.
2. indexed
$ List = array (
1 => "test ",
2 => "test1 ",
3 => "test2 ",
4 => "test3"
);
The default index of the array defined by this method starts from 1.
Note:
1). indexes in an indexed definition array can be strings.
2) print array cannot be used. you need to use print_r to print the array.
The var_dump () function outputs the array in detail.
3 ).
$ List = array (
1 => "test ",
"Test1 ",
"Test2 ",
"Test3"
);
This definition method is the same as the example with an index definition.
3. range function
$ List = range (); // specifies that the array range is between 1 and 10.
$ List = range ('A', 'z'); // you can specify the range of the array in ~ Between z
Note:
In PHP5, the range function has a new usage method.
$ List = range (, 2); // you can specify that the orientation of an array is between 1 and 10, and increase by 2 at a time.
$ List = );
Returns the length of an array.
Count ($ list );
Delete array elements or arrays
1. unset ();
For example:
$ List = array ('1', '2', '3', '4', '5 ');
Unset ($ list [2]);
$ List value: ['1', '2', '4', '5']
If the unset method is used for the entire array, the array is cleared.
2. reset ();
Clear array
Note:
The difference between unset and reset is that
Unset clears the array-related variables and memory, while reset only clears the values in the array, and the variables still exist.
Array merge:
1. array_merge ();
Array_merge ($ list1, $ list2 );
2. $ lists = $ list1 + $ list2
Array sorting:
1. sort ($ list); // values are sorted without keys.
2. rsort ($ list); // values are sorted in reverse order regardless of keys.
3. asort ($ list); // sort the values, but keep the corresponding key value
4. arsort ($ list); // sorts values in reverse order to maintain the corresponding key value
5. ksort ($ list); // Sort keys to maintain the corresponding key value
6. krsort ($ list); // Sort keys to maintain the corresponding key value
Conversion between strings and arrays:
1. convert a string to an array
$ List = explode ("string separator", "string ");
2. convert the array to a string
$ Str = implode ("separator", "array ");
List method
Used to assign the values of array elements to individual variables.
For example:
$ List = array ("1", "2", "3 ");
List ($ one, $ two, $ three) = $ list;
Print $ one. ''. $ two.''. $ three;
Output value: 1 2 3
Note:
The list function only applies to arrays with numeric indexes starting from 0.
When using the list function, you must confirm that each array element is received, but the received variable can be empty.