Home  >  Article  >  Backend Development  >  PHP recursive function Introduction to Array related functions in PHP

PHP recursive function Introduction to Array related functions in PHP

PHP中文网
PHP中文网Original
2017-03-15 15:43:501648browse

1. Use the function array_keys() to get all the keys in the array, parameter: 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 function array_values() to get all the values ​​in the array, parameter: 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 function array_map (), so that each element calls a custom function, parameters: 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 function array_merge() to merge two arrays into one, parameters: array, array
associative array When merging, those with the same key will be overwritten by the subsequent array. When the index arrays are merged, they will be connected together 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 function ksort() to sort according to the key name. Note that no new ones are returned. Arrays are still the original arrays


$arr=array("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3");
ksort($arr);
print_r($arr);
//Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )
使用函数array_search(),搜索某个键值,返回对应的键
$arr=array("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3");
echo array_search("taoshihan1",$arr);
// 1

The above introduces the php recursive function Array related functions in PHP, including the content of php recursive functions. I hope it will be helpful to friends who are interested in PHP.

Related articles:


How to use PHP recursive functions effectively? Typical examples of php recursive functions
A brief analysis of the use of PHP recursive function return values
php recursive function return may not be able to return the desired value correctly

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn