[PHP] Returning to basic (Array-related functions) and returning to array_PHP tutorial

Source: Internet
Author: User
[PHP] returns to basic (Array-related functions) and array. [PHP] returns to basic (Array-related functions) and returns to array using the array_keys () function to obtain all the keys in the Array. parameters: array $ arrarray (); $ arr [one] one; $ arr [two] [PHP] returns to basic (Array-related functions) and array

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 )

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 )

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 )

Use the array_merge () function to combine 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 )

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 and return the corresponding key.

$arr=array("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3");echo array_search("taoshihan1",$arr);// 1

Struct returns to basic (Array-related functions), returns to array, and uses the array_keys () function to obtain all the keys in the array. parameters: Array $ arr = array (); $ arr ['one'] = "one"; $ arr ['two'] =...

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.