Pointer Operations for PHP arrays (summary of PHP array functions)

Source: Internet
Author: User
This article introduces the following, the PHP array of pointer operations related content, is commonly used PHP array functions of the Operation Guide, the need for friends, refer to it.

PHP array operation function: Array (key=value); Create arrays//display array print_r ($array); Create a new array using the compact () function, and make the parameters a unit of the new array; $newArray = compact (Red,green,yellow,blue,array); Use the Extract () function to convert the cells in the array to variable extract ($exArray); echo $key 1 $key 2 $ 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");//Use the Extract () function to convert a cell in an array to a variable extract ($exArray); echo "$key 1 $key 2 $ Key3 $key 4 $key 5 ";

※ Check value, key 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

※ Iterate over the array foreach ($two as $subArray),//iterate over the array while (list ($key, $value) = each ($array)) {echo "$key + = $value,";//use each to iterate through the array}

※ Fill Array//left, right padding array array_pad ($array, +3, "Shuzhi"),//2 parameter is filled from left to right, the value is larger than the number of cells to fill $array1 = Array_fill (5,5, "test");//Use array _fill () fills the value of this array with a value of test, starting with the 5th cell, populating it with a total of 5 cells//padding array key Name $keys = Array (' String ', 5, ten, ' str '); $array 3 = Array_fill_keys ($ Keys, "array values");//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 the value 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 to

※ 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);

※ Merge arrays//use Array_merge () function to merge 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 to the beginning of the array array_unshift ($array, 7,8);

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.