For Web programming, the most important thing is to access and read/write data. There may be many storage methods, such as strings, arrays, and files. Array is an important method in PHP Data Applications. PHP has a large number of array functions. The following is a summary of my learning. I will take this note to facilitate future authentication.
1. array Definition
The array definition uses the array () method to define an empty array:
<? Php $ number = array (1, 3, 5, 7, 9); // defines an empty array $ result = array (); $ color = array ("red", "blue ", "green"); // custom key value $ language = (1 => "English", 3 => "Chinese", 5 => "Franch "); // define a two-dimensional array $ two = array ("color" => array ("red", "blue "), // end with a comma "week" => array ("Monday", "Friday") // No punctuation in the last sentence);?>2. Create an array
Compact ()
Compact () function-converts one or more variables (including arrays) to an array: array compact (mixed $ varname [, mixed $...]).
<?PHP $number = "1,3,5,7,9"; $string = "I'm PHPer"; $array = array("And","You?"); $newArray = compact("number","string","array"); print_r ($newArray);?>
The compact () function is used to convert two or more variables to an array. Of course, it also contains array variables. The parameter is the variable name, not the $ full name. The opposite function is extract (). As the name suggests, it is to convert an array into a single string. The key value is used as the string name, And the array value is used as the string value.
Running result:
Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )
Array_combine ()
Array_combine () -- combines two arrays into an array, one as the key value and the other as the value: array array_combine (array $ keys, array $ values)
<?PHP $number = array("1","3","5","7","9"); $array = array("I","Am","A","PHP","er"); $newArray = array_combine($number,$array); print_r ($newArray);?>
The array_combine function is not much to talk about. Everyone can understand it.
Running result:
Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
Range ()
Range () function -- create an array of the specified range:
<? PHP $ array1 = range (0,100, 10); // 0 indicates the start value, 100 indicates the end value, and 10 indicates the step value (the default step value is 1 ). print_r ($ array1); echo "<br/>"; $ array2 = range ("A", "Z"); print_r ($ array2 ); echo "<br/>"; $ array3 = range ("z", "a"); print_r ($ array3);?>
Array_fill ()
Array_fill () function -- fill Array Function:
<? PHP $ array = range (); $ fillarray = range ("a", "d"); $ arrayFilled = array_fill (, $ fillarray ); // $ fillarray can be a string, such as "test ". echo "<pre>"; print_r ($ arrayFilled); echo "</pre>"; $ keys = array ("string", "2", 9, "SDK ", "PK"); $ array2 = array_fill_keys ($ keys, "testing"); echo "<pre>"; print_r ($ array2 ); echo "</pre>" ;?>
Running 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] => a [1] => b [2] => c [3] => d ) [3] => Array ( [0] => a [1] => b [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. array Traversal
Foreach Traversal
Foreach (array_expression as $ value ){}
Foreach (array_expression as $ key => $ value ){}
<?PHP$speed = array(50,120,180,240,380);foreach($speed as $keys=>$values){echo $keys."=>".$values."<br />";}?>
Running result:
0=>501=>1202=>1803=>2404=>380
While loop Traversal
While loop traversal is generally combined with the list function, the following is an instance
<? PHP $ staff = array ("name", "gender", "Age"), array ("Xiao Zhang", "male", 24), array ("Xiao Wang ", "female", 25), array ("Xiao Li", "male", 23); echo "<table border = 2>"; while (list ($ keys, $ value) = each ($ staff) {list ($ name, $ sex, $ age) = $ value; echo "<tr> <td> $ name </td> <td> $ sex </td> <td> $ age </td> </tr> ";} echo "</table>" ;?>
For Loop Traversal
<?PHP $speed = range(0,220,20); for($i =0;$i<count($speed);$i++) { echo $speed[$i]." "; }?>
Running result:
0 20 40 60 80 100 120 140 160 180 200 220
4. array pointer operation
Involved functions include reset, prev, end, next, current, and each.
Example 1: next and prev
<? PHP $ speed = range (0,220, 20); echo current ($ speed); // output the value of the current position (at the beginning of the array) $ I = rand ); while ($ I --) {next ($ speed); // the pointer moves one digit backward from the current position} echo current ($ speed ); // output the current position value echo "<br/>"; echo prev ($ speed); // output the previous position array value echo "<br/> "; echo reset ($ speed); // reset the pointer to the array and point the pointer to the starting position echo "<br/>"; echo end ($ speed ); // output the array value at the last position echo "<br/>";?>
Running result:
02202000220
Example 2: each function pointer operation
<? PHP $ speed = range (0,200, 40); echo "each implementation pointer down <br/>"; echo "0 speed is ". current (each ($ speed )). "<br/>"; echo "1 speed is ". current (each ($ speed )). "<br/>"; echo "2 speed is ". current (each ($ speed )). "<br/>"; echo "3 speed is ". current (each ($ speed )). "<br/>"; echo "the speed of 4 blocks is ". current (each ($ speed )). "<br/>"; echo "5-way speed is ". current (each ($ speed )). "<br/>"; echo "Use the each function to move the array pointer and traverse the array <br/>"; reset ($ speed ); // point the array pointer to the beginning of the array. While (list ($ key, $ value) = each ($ speed) {echo $ key. "=>". $ value. "<br/>" ;}?>
Running result:
The speed at which the each pointer moves down 0 blocks is 01 block speed is 402 block speed is 803 block speed is 1204 block speed is 1605 block speed is 200 block speed is use each function to implement Array pointer movement, array traversal 0 => 01 => 402 => 803 => 1204 => 1605 => 200
5. array addition and deletion operations
Add an array Member
Example 1: $ num [] = value is directly assigned to the end of the array:
<? PHP $ num = array (1 => 160 => 240, 3 =>); echo "using expressions to add array members <br/>"; $ num [] =; print_r ($ num);?>
Running result:
Use an expression to add an Array member Array ([0] => 80 [1] => 120 [2] => 160 [3] => 240)
Example 2: array_pad function, which selectively appends the first and end of an array
<? PHP $ num = array (1 => 160 => 4,200, 3 =>); $ num = array_pad ($ num ); echo "Use the array_pad function to add members to the end of the array <br/>"; print_r ($ num); echo "<br/> array_pad can also fill the array header <br/> "; $ num = array_pad ($ num,-8, 40); print_r ($ num);?>
Running result:
Use the array_pad function to add a member Array ([0] => 80 [1] => 120 [2] => 160 [3] => 200) to the end of the Array) array_pad can also fill the Array header Array ([0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5] => 120 [6] => 160 [7] => 200)
Example 3: append an inbound stack (array_push ):
<? PHP $ num = array (1 => 160 => 200,240,280, 3 =>); array_push ($ num,); // You can append it by yourself, directly add print_r ($ num) to the end of the array;?>
Running result:
Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )
Example 4: add an array member at the beginning of array_unshift ()
<? PHP $ num = array (1 => 80, 2 => 160, 3 =>); array_unshift ($ num,); // You can append it by yourself, directly add print_r ($ num) to the end of the array;?>
Running result:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
Note: After the array_unshift () function is used, the key value of the array starts from 0!
Delete an array Member
Example 1: unset () command to delete an array member or array:
<? PHP $ num = array_fill (, rand (); print_r ($ num); echo "<br/>"; unset ($ num [4]); print_r ($ num); echo "<br/>"; unset ($ num); if (is_array) {echo "the unset command cannot delete the entire array ";} else {echo "the unset command can delete arrays" ;}?>
Running result: (the running error and description array are also deleted and no longer exist)
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. the php on line 21unset command cannot delete the entire array.
Example 2: The array_splice () function deletes an array member.
<? Php $ a = array ("red", "green", "blue", "yellow"); count ($ a); // get 4 array_splice ($, 1, 1); // Delete the second element count ($ a); // get 3 echo $ a [2]; // get yellow echo $ a [1]; // get blue?>
Example 3: array_unique:
<?php $a=array("red", "green", "blue", "yellow","blue","green"); $result = array_unique($a); print_r($result); ?>
Running result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Example 4: array_merge and array_merge_recursive merge Arrays
<?php $array1 = array("r"=>"red",1,2,3,4); $array2 = array("b"=>"blue",4=>5,6,7,8,9); $array3 = array("r"=>"read",4=>10,2=>11); $array4 = array( array(4=>10), array(7=>13) ); $array5 = array( array(4=>11), array(6=>12) ); $result = array_merge($array1,$array2,$array3,$array4,$array5); echo "<pre>"; print_r($result); echo "</pre>"; $result = array_merge_recursive($array1,$array2,$array3,$array4,$array5); echo "<pre>"; print_r ($result); echo "</pre>"; ?>
Running 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] => 10 [10] => 11 [11] => Array ( [4] => 10 ) [12] => Array ( [7] => 13 ) [13] => Array ( [4] => 11 ) [14] => Array ( [6] => 12 ))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] => 10 [10] => 11 [11] => Array ( [4] => 10 ) [12] => Array ( [7] => 13 ) [13] => Array ( [4] => 11 ) [14] => Array ( [6] => 12 ))
Note: 1. If the key name of array_merge is a number, the index will be re-created. If the same string key name is used, the subsequent key name will overwrite the previous one. 2. The array_merge_recursive function integrates the key list elements of the same string into an array.
6. array key value and value operations
Example 1: in_array () checks whether a value exists in the array.
<? Php $ array = range (); if (in_array (9, $ array) {echo "an array exists" ;}?>
Running result: An array exists.
Example 2: key () gets the current key name of the array:
<?php $array = range(0,9); $num = rand(0,8); while($num--) next($array); $key = key($array); echo $key; ?>
The result of this instance is a dynamic result in the range of (0-8) and is not displayed.
Example 3: The list () function assigns the values in the array to the specified variable:
<? PHP $ staff = array ("name", "gender", "Age"), array ("Xiao Zhang", "male", 24), array ("Xiao Wang ", "female", 25), array ("Xiao Li", "male", 23); echo "<table border = 2>"; while (list ($ keys, $ value) = each ($ staff) {list ($ name, $ sex, $ age) = $ value; echo "<tr> <td> $ name </td> <td> $ sex </td> <td> $ age </td> </tr> ";} echo "</table>" ;?>
Example 4: array_flip () swap the key value and value of the array:
<?PHP $array = array("red","blue","yellow","Black"); print_r($array); echo "<br />"; $array = array_flip($array); print_r($array); ?>
Running result:
Array ( [0] => red [1] => blue [2] => yellow [3] => Black ) Array ( [red] => 0 [blue] => 1 [yellow] => 2 [Black] => 3 )
Example 5: array_keys () and array_values () return all key values and values in the array:
<?PHP $array = array("red","blue","yellow","Black"); $result = array_keys($array); print_r($result); echo "<br />"; $result = array_values($array); print_r($result); ?>
Running result:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 ) Array ( [0] => red [1] => blue [2] => yellow [3] => Black )
Example 6: array_search () Search value:
<? PHP $ array = array ("red", "blue", "yellow", "Black"); $ result = array_search ("red", $ array ); if ($ result = NULL) {echo "no value red";} else {echo "value $ result";}?>
Result: A value of 0 exists.
The value returned by the array_search () function may be false, 0, or NULL. Therefore, be sure to use "=" when determining the value"
7. Sort Arrays
Example 1: Sorting arrays by sort (), rsort ()/asort (), and arsort:
<? PHP $ array = array ("B", "c", "d", "a"); sort ($ array ); // print_r ($ array); echo "<br/>"; rsort ($ array); // Reverse Order print_r ($ array);?>
Result:
Array ( [0] => a [1] => b [2] => c [3] => d ) Array ( [0] => d [1] => c [2] => b [3] => a )
The sort () and rsort () functions sort arrays in ascending order and return bool values;
The asort () and arsort () functions retain the sorting of key values. After sorting, the key values are not re-indexed.
Example 2: disrupt the array order -- shuffle () function:
<? PHP $ array = array ("a", "B", "c", "d"); shuffle ($ array ); // print_r ($ array);?>
The result is dynamic:
Array ( [0] => c [1] => a [2] => d [3] => b )
The shuffle result is a bit random, and each refresh is different.
Example 3: reverse direction of the array_reverse () array:
<? PHP $ array = array ("d", "B", "a", "c"); $ array = array_reverse ($ array ); // print_r ($ array);?>
Running result:
Array ( [0] => c [1] => a [2] => b [3] => d )
Example 4: Natural sorting algorithms natsort () and natcasesort ();
<? PHP $ array = array ("sort2", "Sort5", "sort1", "sort4"); natsort ($ array ); // print_r ($ array); echo "<br/>"; natcasesort ($ array); print_r ($ array);?>
Result:
Array ( [1] => Sort5 [2] => sort1 [0] => sort2 [3] => sort4 ) Array ( [2] => sort1 [0] => sort2 [3] => sort4 [1] => Sort5 )
Natsort () and natcasesort () are used to sort arrays in a natural way. Natcasesort ignores case sensitivity.
Example 5: sort the key values of the array ksort ():
<? PHP $ array = array (1 => "sort2", 4 => "Sort5", 2 => "sort1", 3 => "sort4 "); ksort ($ array); // print_r ($ array) in descending order;?>
Result:
Array ( [1] => sort2 [2] => sort1 [3] => sort4 [4] => Sort5 )
Note: The ksort () function re-creates an index.
8. Other array usage
Cout ($ array) -------- count the number of units in the array array_diff ($ array1, $ array2) ---------- calculate the differences between arrays, and return the results that are included in the first array but not in the second array. Array_diff_assoc ($ array1, $ array2) --------- same as array_diff (), but its key value is also compared with array_diff_key ($ array1, $ array2) ---------- compare the key value array_product) ----------- return the product array_sum ($ array) -------------- of all values and array_rand ($ array, $ n) ---------- get $ n values in the $ array, returns the array array_intersect ($ array1, $ array2) -------------- obtains the intersection between the two arrays ($ array1, $ array2) --------------- performs key-Value Comparison Using array_intersect ($ array1, $ array2) --------------- compare the intersection of two array key values
Summary
The use of arrays is crucial in PHP. Because PHP does not have pointers, arrays undertake a large amount of data operation tasks. Learning arrays well can make PHP applications handy. All the functions and usage related to PHP arrays are listed here. Please study them together!