For Web programming, the most important thing is to access and read and write data. There may be many ways to store them, such as strings, arrays, files, and so on. arrays, which can be said to be a more important way of data application in PHP. PHP array functions are numerous, the following is my study of the summary, take this to remember, easy to learn later.
1. Array definitions
The definition of an array is defined using the array () method, and an empty array can be defined:
"中文版",3=> "Chinese",5=> "Franch"); Define a two-dimensional array $two = Array (" Color" =>array ("Red", "Blue"), //End with a comma "Week" =>array ("Monday", " Friday ") //Last sentence without punctuation );?>
2. Creating an array
Compact ()
Compact () function--converts one or more variables (containing arrays) to an array: the array compact (mixed $varname [, mixed $ ...]).
The compact () function is used to convert two or more variables to arrays and, of course, to array variables. Its argument is the name of the variable, not the full name. The opposite function is that extract () functions as the name implies to convert an array to a single string, a key value as its string names, and an array value as the value of a string.
Operation Result:
Array ([number] = 1,3,5,7,9 [string] + I ' m phper [array] = = Array ([0] = = and [1] = =))
Array_combine ()
Array_combine ()--two arrays are re-formed into an array, one for key values: Array array_combine (array $keys, array $values)
Array_combine function not much to say, who read all understand.
Operation Result:
Array ([1] = I [3] = Am [5] = A [7] = = PHP [9] = er)
Range ()
The range () function--Creates an array of the specified range:
"; $array 2 = Range ("A", "Z"); Print_r ($array 2); echo "
"; $array 3 = range ("Z", "a"); Print_r ($array 3);? >
Array_fill ()
Array_fill () Function--fills the array function:
"; Print_r ($arrayFilled); echo "
"; $keys = Array ("string", "2", 9, "SDK", "PK"); $array 2 = Array_fill_keys ($keys, "testing"); echo "
"; Print_r ($array 2); echo "
";? >
Operation Result:
Array ( [0] = = Array ( [0] = a [1] = b [2] = = C [3] + D ) [1] + = array ( [0] = a [1] = b [2] = C [3] + D ) [2] = = Array ([0] =&G T A [1] = b [2] = C [3] + D ) [3] = = Array ( [0] = a [1] = b< C25/>[2] = c [3] = + D ) [4] = = Array ( [0] = = a [1] = b [2] = + C
[3] (+ D )) Array ( [string] = Testing [2] = testing [9] = = Testing [SDK] = Testing [PK] = testing)
3. Iterating the array
foreach traversal
foreach (array_expression as $value) {}
foreach (array_expression as $key = $value) {}
$values) {echo $keys. = ". $values."
";}? >
Operation Result:
0=>501=>1202=>1803=>2404=>380
While loop traversal
While loop traversal is generally combined with the list function, the following is an example
"; while (list ($keys, $value) = each ($staff)) { list ($name, $sex, $age) = $value; echo "$name $sex$age"; } echo ""; ? >
For loop traversal
Operation Result:
4. Pointer manipulation of arrays
The functions involved include reset, prev, end, next, current, each.
Example one: Next and prev
"; Echo prev ($speed);//Output previous position array value echo "
"; echo Reset ($speed);//Resets the pointer to the array, pointing the pointer to the start position echo "
"; Echo End ($speed);//output array value of last position echo "
";? >
Operation Result:
02202000220
Example two: Each function pointer operation
"; echo "0-block speed is". Current ($speed). "
"; echo "1-block speed is". Current ($speed). "
"; echo "2-block speed is". Current ($speed). "
"; echo "3-block speed is". Current ($speed). "
"; echo "4-block speed is". Current ($speed). "
"; echo "5-block speed is". Current ($speed). "
";
"; Reset ($speed);//This is where the array pointer is pointed to the array first while (list ($key, $value) =each ($speed)) { echo $key. = ". $value."
"; }? >
Operation Result:
Each implementation of the pointer down 0 block speed is 01 block speed is 402 block speed is 803 block speed is 1204 block speed is 1605 block speed is 200 use each function to implement array pointer movement, array traversal 0=>01=>402=>803= >1204=>1605=>200
5. Addition and deletion of arrays
Adding array members
Instance one: $num [] = value is appended directly to the end of the array:
80,2=>120,3=>160); echo "Adding an array member using an expression
"; $num []=240; Print_r ($num); ? >
Operation Result:
To add an array member using an expression array ([0] = [1] = [2] = [3] = 240)
Example two: Array_pad function, array array, optional append
80,2=>120,3=>160); $num = Array_pad ($num, 4,200); echo "Use the Array_pad function to add members to the end of the array
"; Print_r ($num); echo "
Array_pad can also populate the array header
"; $num = Array_pad ($num, -8,40); Print_r ($num); ? >
Operation Result:
Add members to the end of the array using the Array_pad function array ([0] = [1] = [2] = [3] = 0) Array_pad can also populate array header arrays ([] => ; [1] [+] = [2] = [3] = + [4] = [5] = [6] = [7] = 200)
Example three: Append to stack operation (Array_push):
80,2=>120,3=>160); Array_push ($num, 200,240,280);//can append itself, directly add to the end of the array print_r ($num); >
Operation Result:
Array ([1] = [2] = [3] = [4] = [5] = [6] = 280)
Example four: Array_unshift () adds an array member at the beginning
80,2=>120,3=>160); Array_unshift ($num, 0,40);//can append itself, directly add to the end of the array print_r ($num); >
Operation Result:
Array ([0] = 0 [1] = [2] = [3] = [4] = 160)
Note: After using the Array_unshift () function, the array's key values will start at 0!
Subtract array members
Instance one: the unset () command deletes an array member or array:
"; Unset ($num [4]); Print_r ($num); echo "
"; Unset ($num); if (Is_array) { echo "unset command cannot delete entire array"; } else{ echo "unset command can delete array"; } ? >
Run Result: (Run error and description array also deleted, no longer present)
Array ([0] = 9 [1] = 9 [2] = 9 [3] = 9 [4] = 9) Array ([0] = 9 [1] = 9 [2] = 9 [3] => ; 9) Notice:use of undefined constant is_array-assumed ' is_array ' in H:\wamp\www\testing\editorplus\test.php on line 21u Nset command cannot delete entire array
Example two: Array_splice () function to delete an array member
Example three: Array_unique delete duplicate values in an array:
Operation Result:
Array ([0] = red [1] = green [2] = = Blue [3] = yellow)
Example four: Array_merge, Array_merge_recursive merged array
"Red", 1,2,3,4); $array 2 = Array ("b" = "blue", 4=>5,6,7,8,9); $array 3 = Array ("r" = "read", 4=>10,2=>11); $array 4 = Array ( array (4=>10), Array (7=>13) ); $array 5 = Array ( array (4=>11), Array (6=>12) ); $result = Array_merge ($array 1, $array 2, $array 3, $array 4, $array 5); echo ""; Print_r ($result); echo "
"; $result = array_merge_recursive ($array 1, $array 2, $array 3, $array 4, $array 5); echo ""; Print_r ($result); echo "
"; ? >
Operation Result:
Array ([r] = read [0] = 1 [1] = 2 [2] = 3 [3] = 4 [b] = = blue [4] = 5 [ 5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = [ten] = [one] = = Array ( [4] = [+] = Array ([7] = =) [] = = Array ( [4] = +) [+] = Array ([6] = =)) array ([r] = array ([0] = = Red [1] = read) [0] = 1 [1] = 2 [2] = 3 [3] => ; 4 [b] = blue [4] = 5 [5] = 6 [6] = 7 [7] = 8 [8] = 9 [9] = [Ten] =&G T One [one] = = Array ([4] = =) [Ten] = = Array ([7] = 13 ) [+] = Array ([4] = =) [+] = Array ([6] = 12 ))
Note: 1. The Array_merge key name is the number that will be re-indexed, and when the same string key name is encountered, the following will overwrite the previous one. 2. The role of the array_merge_recursive function is to synthesize an array of key-list elements of the same string.
6. Key and value operations for arrays
Instance one: In_array () detects if a value exists in the array
Run Result: the array exists
Example two: Key () Gets the current key name of the array:
The result of this instance is a dynamic result, range (0-8), and no result is shown.
Example three: the list () function assigns the values in the array to the specified variable:
"; while (list ($keys, $value) = each ($staff)) { list ($name, $sex, $age) = $value; echo "$name $sex$age"; } echo "";? >
Example four: Array_flip () swaps the key values and values of an array:
"; $array = Array_flip ($array); Print_r ($array); ? >
Operation Result:
Array ([0] = red [1] = blue [2] = yellow [3] = Black) array ([red] = 0 [Blue] = 1 [yellow] => ; 2 [Black] = 3)
Example five: Array_keys (), array_values () returns all the key values and values in the array:
"; $result = Array_values ($array); Print_r ($result); ? >
Operation Result:
Array ([0] = 0 [1] = 1 [2] = 2 [3] = 3) array ([0] = + red [1] = blue [2] = + YELLOW [3] = = Black)
Example six: Array_search () search value:
Result: There is a value of 0
The value returned by the function Array_search () may be false or 0 or null, so be careful to use "= = =" When judging
7. Sorting of arrays
Example one: Sort (), Rsort ()/asort (), and Arsort () are sorted by array:
"; Rsort ($array);//Reverse sort print_r ($array);? >
Results:
Array ([0] = a [1] = b [2] = = c [3] + D) Array ([0] + d [1] = c [2] = b [3] = a)
The sort (), rsort () function sorts the array from low to high, and returns the result as a bool value;
The Asort (), Arsort () functions are sort of reserved key values, and the key values are not re-indexed after sorting.
Example two: Disrupting the array order--shuffle () function:
The result is a dynamic result:
Array ([0] = + c [1] = a [2] + d [3] = b)
The result of the shuffle is a bit random meaning that each refresh is different.
Example three: Array_reverse () array Reverse:
Operation Result:
Array ([0] = c [1] = a [2] = = b [3] + D)
Example four: Natural sorting algorithm--natsort () and Natcasesort ();
"; Natcasesort ($array); Print_r ($array);? >
Results:
Array ([1] = SORT5 [2] = sort1 [0] = sort2 [3] = sort4) Array ([2] = = Sort1 [0] = = sort2 [3] => ; SORT4 [1] = SORT5)
Natsort (), Natcasesort () is a natural sort of array, which uses the normal sorting algorithm of numbers. Natcasesort ignores case.
Example five: Sorting an array of key values Ksort ():
"Sort2",4=> "Sort5",2=> "Sort1",3=> "Sort4"); Ksort ($array);//low to High sort print_r ($array);? >
Results:
Array ([1] = Sort2 [2] = Sort1 [3] = = SORT4 [4] = = SORT5)
Note: the Ksort () function re-established the index.
8. Other uses of arrays
cout ($array)--------The number of cells in the Array_diff ($array 1, $array 2)----------the different points between the statistical arrays, returning the first array and none in the second array. ARRAY_DIFF_ASSOC ($array 1, $array 2)---------with Array_diff (), except that it also compares Array_diff_key ($array 1, $array 2) for the key value------------ The comparison key value Array_product ($array)-----------returns the product Array_sum ($array) of all the numbers of the array--------------all values and Array_rand ($array, $n)----- The-----takes $n values in the $array array, returns the array array_intersect ($array 1, $array 2)----------------gets the intersection of two arrays Array_intersect_assoc ($ Array1, $array 2)---------------The comparison of key values on the basis of Array_intersect Array_intersect_key ($array 1, $array 2)----------------- Compare the intersection of two array key values
Summarize
The use of arrays is critical in PHP, because PHP has no pointers, so the array takes on a lot of data manipulation tasks. Learn the array, in order to get the handy PHP application, listed here are commonly used PHP array related functions and usage, welcome to learn together!
http://www.bkjia.com/PHPjc/752575.html www.bkjia.com true http://www.bkjia.com/PHPjc/752575.html techarticle for Web programming, the most important thing is to access and read and write data. There may be many ways to store them, such as strings, arrays, files, and so on. Array, which can be said to be the number of PHP ...