PHP Learning Notes-arrays (4)

Source: Internet
Author: User

Array_key_exists (Key,array);

Role:

Whether there is a key in array arrays

Return:

BOOL exists return True, there is no return false

Example: whether the name exists in the array

$arr = Array (' name ' = = ' char ', ' age ' = +, ' sex ' = ' f '); if (array_key_exists (' name ', $arr)) {echo ' name exists ';} else {echo ' name does not exist ';} Output: Name exists


Array_keys (array, value, BOOL);

Role:

Gets the key value in the array

Parameters:

Array: Arrays

Value: Optional, specifying the value in the array

BOOL: Optional default not false, used with value values, default does not need to match the type of value, true matches the type.

return value:

Returns an array containing the key

Example:

$arr = Array (' name ' = = ' char ', ' age ' = +, ' sex ' = ' f ');       $keys = Array_keys ($arr); Gets the key value of the array $key 1 = Array_keys ($arr, ' 21 '); Gets the value 21 for the key $key 2 = Array_keys ($arr, ' + ', true); Gets the key value of value 21, and value is the string Print_r ($keys); Print_r ($key 1); Print_r ($key 2); Output: Array ([0] + = name [1] = age [2] = = Sex) array ([0] = age) Array ()


Array_map (string, array);

Role:

With a custom function, each parameter in the array is processed and returned

Parameters:

String: Name of function

Array: Arrays

return value:

Returns the new array

Example: Multiply each parameter of an array by 2

function MyFunction ($v) {return $v *;} $arrOne = Array (' A ' =>1, ' b ' = = 2, 3, 4, 5); $res = Array_map (' myFunction ', $arrOne); Print_r ($res); Output: Array ([A] = 2 [b] = 4 [0] = 6 [1] = 8 [2] = 10)


Array_merge (Array1, array2);

Role:

Merging one or more arrays

Parameters:

Array1: Merged array 1

Array2: merged array 2

return value:

The merged array

Example:

$arrOne = Array (' a ' = = ' Blue ', ' b ' = ' yellow '); $arrTwo = Array (' a ' = = ' red ', ' d ' = ' purple '); $res = Array_merge ($arrOne, $ARRTWO); Print_r ($res); Output: Array ([a] = red [b] = yellow [d] = purple)


Array_merge_recursive (Array1, arrray2);

Role:

Recursive merging of multiple arrays, unlike Array_merge (), which does not overwrite when processing the value of the same key name

Parameters:

Array1: Merged array 1

Aray2: merged array 2

return value:

The merged array

Example:

$arrOne = Array (' a ' = = ' Blue ', ' b ' = ' yellow '); $arrTwo = Array (' a ' = = ' red ', ' d ' = ' purple '); $resRe = Array_merge_recursive ($arrOne, $ARRTWO); Print_r ($resRe);    Output: Array ([a] = = Array ([0] = blue [1] = red) [b] = Yellow [d] = purple)



This article from "Snail Crawl" blog, please be sure to keep this source http://10130617.blog.51cto.com/10120617/1890338

PHP Learning Notes-arrays (4)

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.