In PHP applications, arrays are the most widely used data structures. In this case, you can master most of PHP by mastering arrays. Next we will summarize the Array Operations of PHP, which may be an array operation that you do not understand or do not know much about.
Returns the internal pointer of the array to one position:
/*** Returns the internal pointer of the array to a position * @ param array & $ arr * @ return mixed to return the value of the previous unit, returns FALSE if no more units exist. * If the array contains null units, or if the unit value is 0, this function returns FALSE when encountering these units * to correctly traverse arrays that may contain null cells or whose unit value is 0, see each () function */mixed prev (array & $ arr); <? Php $ transport = array ('foot', 'bike', 'car', 'plane '); $ mode = current ($ transport ); // $ mode = 'foot'; $ mode = next ($ transport); // $ mode = 'bike '; $ mode = next ($ transport ); // $ mode = 'car'; $ mode = prev ($ transport); // $ mode = 'bike '; $ mode = end ($ transport ); // $ mode = 'plane '; $ mode = reset ($ transport); // $ mode = 'foot';?>
Convert cells in the array to variables:
Array ("key" => "value"); Create an array // display the array print_r ($ array); // use the compact () function to create an array, use the parameter as the unit of the new array; $ newArray = compact ("red", "green", "yellow", "blue", "array "); // use the extract () function to convert the cells in the array to the variable extract ($ exArray); echo "$ key1 $ key2 $ key3 $ key4 $ key5 ";
Check value and key:
Array_key_exists ($ key, $ array); // check the array key in_array ($ value, $ array); // check the value in the array
Get value:
// Use array_values () to obtain the array value $ carValues = array_values ($ car); // retrieve the array key name $ twoKeys = array_keys ($ two ); key ($ array); // output the key name of the current Unit // After defining the array, use current () to get the value of the current Unit $ red = current ($ array ); list ($ red, $ green) = $ array; // assign values in the array to variables, $ array = array ("red", "green "); each ($ two); // returns the key and value of the current cell in the array.
Traverse the array:
Foreach ($ two as $ subArray); // traverses the array while (list ($ key, $ value) = each ($ array) {echo "$ key => $ value, "; // use each to traverse the array}
Fill array:
// Fill the array array_pad ($ array, + 3, "shuzhi") to the left and right; // The parameter 2 is filled from left to right, $ array1 = array_fill (5, 5, "test"); // use array_fill () to fill in the value of this array. The value is test, starting from 5th units, fill in a total of 5 units // fill in the array key name $ keys = array ('string', 5, 10, 'str '); $ array3 = array_fill_keys ($ keys, "array value"); // use the array_filp () function to exchange key names and values $ speed = array_flip ($ speed ); // use the array_splice () function to replace the value of 6th units with 7 $ output = array_splice ($ input, 6, 0, 7); // use the array_splice () function to delete the array unit, only the first five units $ output = array_splice ($ input, 5); $ array1 = range (10,100, 10); // use the third parameter of the range () function, set step values between units
Sort:
Shuffle ($ array); // disrupt the array order // use array_multisort () to sort the three arrays array_multisort ($ sort1, $ sort2, $ sort3 ); // sort the array and maintain the index relationship asort ($ array); // sort the test array in reverse order and maintain the index relationship arsort ($ array ); // use ksort () to sort the array key names ksort ($ array); // use the krsort () function to sort the key names in reverse order krsort ($ array); // use sort () sort the test array [by key name] sort ($ array); // use natsort () to sort [natural sorting, sort by values] case sensitive to unit values natsort ($ array); // use the natcasesort () function to sort [natural sorting], but ignore the value case sensitive natcasesort ($ array ); // use the array_reverse () function to sort the array elements in reverse order $ newArray = array_reverse ($ array, TRUE); // retain the original key name when set to TRUE
Intersection and difference set:
// Use array_diff () to calculate the difference set of the three arrays [Comparison of array values] $ result = array_diff ($ dog1, $ dog2, $ dog3); // use array_diff_assoc () calculate the difference set of the three arrays [Comparison of values and key names] $ result = array_diff_assoc ($ dog1, $ dog2, $ dog3); // use array_diff_key () calculate the difference set of the three arrays [comparison key name] $ result = array_diff_key ($ dog1, $ dog2, $ dog3); // use array_intersect () calculate the intersection of the three arrays [compare the array values] $ result = array_intersect ($ dog1, $ dog2, $ dog3); // use array_intersect_assoc () calculate the intersection of the three arrays [Comparison of values and key names] $ result = array_intersect_assoc ($ dog1, $ dog2, $ dog3); // use array_intersect_key () calculate the intersection of the three arrays [comparison key name] $ result = array_intersect_key ($ dog1, $ dog2, $ dog3 );
Merge Arrays:
// Use the array_merge () function to merge arrays $ result = array_merge ($ array1, $ array2, $ array3, $ array4, $ array5); array_rand ($ input, 10 ); // randomly retrieve 10 units count ($ array, COUNT_RECURSIVE); // display the number of array units. 2 parameters can only be 1 or COUNT_RECURSIVE, and sometimes multi-dimensional arrays can be traversed.
Stack Entry/Exit:
// Array output stack, followed by first-in-first-out, the last unit of the array pops up array_pop ($ array); // array into the stack, add the 7, 8 values to the end of the array array_push ($ array, ); // remove the elements starting with the array from the array array_shift ($ array); // Add to the beginning of the array array_unshift ($ array );