Introduction to Array functions in php recursive functions

Source: Internet
Author: User
Introduction to Array-related functions in php recursive functions 1. use the array_keys () function to obtain all the keys in the Array. parameters: Array

$arr=array();$arr['one']="one";$arr['two']="two";$arr['three']="three";$newArr=array_keys($arr);print_r($newArr);//Array ( [0] => one [1] => two [2] => three )

2. use the array_values () function to obtain all values in the array. parameters: Array

$arr=array();$arr[20]="one";$arr[30]="two";$arr[40]="three";$newArr=array_values($arr);print_r($newArr);//Array ( [0] => one [1] => two [2] => three )

3. use the array_map () function to make each element call a custom function. parameter: String type function name, array

$arr=array();$arr[0]="one";$arr[1]="two";$arr[2]="three";function test($v){  return $v." Hello";}$newArr=array_map("test",$arr);print_r($newArr);//Array ( [0] => one Hello [1] => two Hello [2] => three Hello )

4. use the array_merge () function to combine the two arrays into one. parameters: Array and array
When the associated array is merged, the same key will be overwritten by the following array
When the index array is merged, it is connected to form a new array.

$arr=array();$arr[0]="one";$arr[1]="two";$arr[2]="three";$arr1=array();$arr[3]="taoshihan1";$arr[4]="taoshihan2";$arr[5]="taoshihan3";$newArr=array_merge($arr,$arr1);print_r($newArr);//Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 )$arr=array("one","two","three");$arr1=array("4","5","6");$newArr=array_merge($arr,$arr1);print_r($newArr);//Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )

5. use the ksort () function to sort by key name. Note that no new array is returned, or the original array.

$ Arr = array ("2" => "taoshihan2", "1" => "taoshihan1", "3" => "taoshihan3"); ksort ($ arr ); print_r ($ arr); // Array ([1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3) use the array_search () function to search for a key value, returns the corresponding key $ arr = array ("2" => "taoshihan2", "1" => "taoshihan1", "3" => "taoshihan3 "); echo array_search ("taoshihan1", $ arr); // 1

The above introduces the Array-related functions of php recursive functions, including the PHP recursive functions, and hopes to help those who are interested in php.

Related articles:
How does a php recursive function work? Typical examples of php recursive functions
Analysis on the use of PHP recursive function return values
Php recursive function return may fail to return the expected value 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.