In PHP applications, arrays are the most widely used data structures. Basically you can say that you can master the majority of PHP by mastering the array skillfully. The following is a summary of the PHP array operations, perhaps you do not understand or do not understand the array operation.
Rewind the internal pointer of the array back to one:
/*** the internal pointer of the array back to a * @param array & $arr * @return Mixed returns the value of the previous cell when no more cells are returned false* if the array contains empty cells, or the value of the cell is 0 The function also returns false* to correctly traverse an array that may contain empty cells or a cell value of 0, see each () function */mixed prev (array & $arr);
Convert the cells in the array to variables:
Array ("Key" = "value"); Create array//display array print_r ($array);//Use the compact () function to create a new array and set the parameters as a unit of the new array; $newArray = Compact ("Red", " Green "," yellow "," Blue "," array "), or//Use the Extract () function to convert the cells in the array to variable extract ($exArray); echo" $key 1 $key 2 $key 3 $key 4 $key 5 ";
Check values, keys:
Array_key_exists ($key, $array);//check array key In_array ($value, $array);//check value in array
Get Value:
Use Array_values () to get the value of the array $carvalues = Array_values ($car);//Remove the key name of the array $twokeys = Array_keys ($two); key ($array);// After outputting the key name//array definition for the current cell, use current () to get the value of the present cell $red = $array), list ($red, $green) = $array;//Assign the value in the array to the variable, $array = Array (" Red "," green "), each ($two);//returns the key and value of the current cell in the array
To iterate over an array:
foreach ($two as $subArray);//traversal array while (list ($key, $value) = each ($array)) {echo "$key + = $value,";//use each to iterate over an array}
To populate an array:
Fills the array array_pad ($array, +3, "Shuzhi") to the left and right, the//2 parameter is filled from left to right, the value is larger than the number of cells, $array1 = Array_fill (5,5, "test");//Use Array_fill () populate the value of this array with a value of test, starting with the 5th cell and populating it with a total of 5 cells//padding array key Name $keys = Array (' String ', 5, ' str '); $array 3 = Array_fill_keys ($keys, " Array value ");//Use the ARRAY_FILP () function to exchange the key name with the value $speed = Array_flip ($speed);//Use the Array_splice () function to replace the 6th cell with a value of 7$output = Array_splice ($ input,6,0,7);//Use the Array_splice () function to delete the array cell, leaving only the first 5 cells $output = Array_splice ($input, 5); $array 1 = range (10,100,10); Use the third parameter of the range () function to set the stepping value between cells
Sort:
Shuffle ($array);//The array order is scrambled//using Array_multisort () to sort three arrays Array_multisort ($sort 1, $sort 2, $sort 3);//Sort this array, And keep the index relationship asort ($array);//reverse-order the test array and keep the index relationship arsort ($array);//Use Ksort () to sort Ksort ($array) with the name of the group key;//Use Krsort () Function Key Name reverse sort krsort ($array);//Sort the test array using sort () []sort ($array) with the key name,//Use Natsort () to sort [natural sort, in numerical order] for cell value case sensitivity Natsort ($ array);//Use the Natcasesort () function to sort [natural sort] but ignore the numeric case natcasesort ($array);//Use the Array_reverse () function to sort the array cells in reverse order $newarray = Array_reverse ($array, TRUE);//true preserve the original key name when set
Intersection, Difference set:
Use Array_diff () to calculate the difference set of three arrays [logarithmic group numerical comparison] $result = Array_diff ($dog 1, $dog 2, $dog 3);//Use ARRAY_DIFF_ASSOC () to calculate the difference set of three arrays [ Compare values and key names] $result = Array_diff_assoc ($dog 1, $dog 2, $dog 3);//Use Array_diff_key () to calculate the difference of three arrays [comparison key name] $result = Array_diff_key ($dog 1, $dog 2, $dog 3);//Use Array_intersect () to calculate the intersection of three arrays [logarithmic group numeric comparison] $result = Array_intersect ($dog 1, $dog 2, $dog 3);// Use ARRAY_INTERSECT_ASSOC () to calculate the intersection of three arrays [comparison of numeric and key names] $result = Array_intersect_assoc ($dog 1, $dog 2, $dog 3);//Use Array_ Intersect_key () calculates the intersection of three arrays [comparison key name] $result = Array_intersect_key ($dog 1, $dog 2, $dog 3);
Merging arrays:
Use the Array_merge () function to combine arrays $result = Array_merge ($array 1, $array 2, $array 3, $array 4, $array 5), Array_rand ($input, 10);// Randomly remove 10 units COUNT ($array, count_recursive);//display array unit number, 2 parameter can only be 1 or count_recursive, sometimes can traverse multidimensional array
Access stack:
Array out of the stack, LIFO, the last element of the array pops up Array_pop ($array);//array into the stack, add 7, 82 values to the end of the array Array_push ($array, 7,8);//move the array from the beginning of the cell to the group Array_shift ( $array);//Add 7, 8 into the array at the beginning of Array_unshift ($array, 7,8);
http://www.bkjia.com/PHPjc/752385.html www.bkjia.com true http://www.bkjia.com/PHPjc/752385.html techarticle in PHP applications, arrays are the most widely used data structures. Basically you can say that you can master the majority of PHP by mastering the array skillfully. The following is an array of PHP operations ...