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

PHP recursive function Introduction to Array related functions in PHP

PHP中文网
Release: 2023-02-28 21:08:01
Original
1686 people have browsed it

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 )
Copy after login

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 )
Copy after login

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 )
Copy after login

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 )
Copy after login

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
Copy after login
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

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template