PHP Recursive function Introduction to the array correlation function in PHP

Source: Internet
Author: User
1. Using the function Array_keys (), get all the keys in the array, parameters: array

$arr =array (); $arr [' One ']= "one"; $arr [' One ', ']= ' and ' '; $arr [' Three ']= ' three '; $newArr =array_keys ($arr);p Rint_r ($ NEWARR);//array ([0] = one [1] = [2] = three)

2. Use the function array_values () to get all the values in the array, parameters: array

$arr =array (), $arr [20]= "One", $arr [30]= "," "$arr", "40]=" three "; $newArr =array_values ($arr);p rint_r ($NEWARR);// Array ([0] = one [1] = [2] = three)

3. Use the function Array_map () so that each element invokes a custom function, parameter: String type function name, array

$arr =array (), $arr [0]= "One", $arr [1]= "," "$arr", "2]=" three "; function test ($v) {  return $v." Hello ";} $NEWARR =array_map ("test", $arr);p Rint_r ($NEWARR);//array ([0] = one hello [1] = both hello [2] = three Hello)

4. Using the function Array_merge (), combine two arrays into one, parameter: array, array
When associative arrays are merged, the same keys are overwritten by subsequent arrays
When an indexed array is merged, it joins together to form a new array

$arr =array (); $arr [0]= "one"; $arr [1]= "," $arr "," 2]= "three"; $arr 1=array (); $arr [3]= "Taoshihan1"; $arr [4]=] Taoshihan2 "; $arr [5]=" taoshihan3 "; $newArr =array_merge ($arr, $arr 1);p rint_r ($NEWARR);//array ([0] = one [1] = = Both [2] = three [3] = taoshihan1 [4] = taoshihan2 [5] = taoshihan3) $arr =array ("One", "one", "three"); $arr 1 = Array ("4", "5", "6"), $newArr =array_merge ($arr, $arr 1);p rint_r ($NEWARR);//array ([0] = one [1] = [2] = three [3] = 4 [4] = 5 [5] = 6)

5. Use the function Ksort (), Sort by key name, notice that no new array is returned, or the original array

$arr =array ("2" = "Taoshihan2", "1" = "Taoshihan1", "3" = "taoshihan3"), Ksort ($arr);p rint_r ($arr);//array ([ 1] = taoshihan1 [2] = taoshihan2 [3] = = TAOSHIHAN3) use function Array_search (), search for a key value, return the corresponding key $arr=array ("2" = " Taoshihan2 "," 1 "=" Taoshihan1 "," 3 "=" taoshihan3 "), Echo array_search (" Taoshihan1 ", $arr);//1

The above describes the PHP recursive function in PHP, the array related functions, including the PHP recursive function content, I hope to be interested in PHP friends helpful.

Related articles:
How does php recursive function work? Typical examples of PHP recursive functions
Analysis of the use of return value of PHP recursive function
PHP recursive function return occurs when the desired value is not returned correctly

  • 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.