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

Mar 15, 2017 pm 03:43 PM

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the optimization techniques for C++ recursive functions? What are the optimization techniques for C++ recursive functions? Apr 17, 2024 pm 12:24 PM

To optimize the performance of recursive functions, you can use the following techniques: Use tail recursion: Place recursive calls at the end of the function to avoid recursive overhead. Memoization: Store calculated results to avoid repeated calculations. Divide and conquer method: decompose the problem and solve the sub-problems recursively to improve efficiency.

How to solve the excessive function nesting error in Python code? How to solve the excessive function nesting error in Python code? Jun 25, 2023 pm 12:35 PM

Python is a very powerful programming language, and many programmers choose Python as their main programming language. However, too much function nesting in the code can make the program difficult to maintain and understand. This article will explore how to solve the excessive function nesting error in Python code. A brief introduction to function nesting Function nesting refers to the process of defining another function in the body of a function. Function nesting can make the structure of the program clearer and the code easier to read and maintain. However, too many nested functions can lead to an overly complex code structure.

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

What are the exit conditions for a recursive function in C++? What are the exit conditions for a recursive function in C++? Apr 17, 2024 am 11:33 AM

The exit conditions of C++ recursive functions include: Baseline conditions: Check whether the function reaches a state that can directly return results, usually judging whether a certain condition or parameter value meets the threshold. Recursion termination condition: Alternative to or in addition to the baseline condition, ensuring that the function stops after a certain number of recursive calls, by tracking the recursion depth or setting a maximum recursion depth limit.

Application of C++ recursive functions in search algorithms? Application of C++ recursive functions in search algorithms? Apr 17, 2024 pm 04:30 PM

Recursive functions are used in search algorithms to explore tree-like data structures. Depth-first search uses a stack to explore nodes, while breadth-first search uses a queue to traverse layer by layer. In practical applications, such as finding files, recursive functions can be used to search for a given file in a specified directory.

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to implement the tail recursion optimization strategy of C++ recursive functions? How to implement the tail recursion optimization strategy of C++ recursive functions? Apr 17, 2024 pm 02:42 PM

The tail recursion optimization strategy effectively reduces the function call stack depth and prevents stack overflow by converting tail recursive calls into loops. Optimization strategies include: Detect tail recursion: Check whether there are tail recursive calls in the function. Convert functions into loops: Use loops instead of tail-recursive calls and maintain a stack to save intermediate state.

See all articles