Table of Contents
[PHP] Return to the basics (Array related functions), return to array
Home Backend Development PHP Tutorial [PHP] Return to the basics (Array related functions), return to array_PHP tutorial

[PHP] Return to the basics (Array related functions), return to array_PHP tutorial

Jul 12, 2016 am 08:52 AM
return value

Use the function array_keys() to get all the keys in the array, parameters: array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>['one']="one"<span>;
</span><span>$arr</span>['two']="two"<span>;
</span><span>$arr</span>['three']="three"<span>;

</span><span>$newArr</span>=<span>array_keys</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Copy after login

Use the function array_values() to get all the values ​​in the array. Parameters: array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[20]="one"<span>;
</span><span>$arr</span>[30]="two"<span>;
</span><span>$arr</span>[40]="three"<span>;

</span><span>$newArr</span>=<span>array_values</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three )</span>
Copy after login

Use the function array_map() to call a custom function for each element. Parameters: String type function name, array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;
</span><span>function</span> test(<span>$v</span><span>){
    </span><span>return</span> <span>$v</span>." Hello"<span>;
}
</span><span>$newArr</span>=<span>array_map</span>("test",<span>$arr</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one Hello [1] => two Hello [2] => three Hello )</span>
Copy after login

Use the function array_merge() to merge two arrays into one, parameters: array, array

When associative arrays are merged, those with the same key will be overwritten by the subsequent array

When index arrays are merged, they will be connected together to form a new array

<span>$arr</span>=<span>array</span><span>();
</span><span>$arr</span>[0]="one"<span>;
</span><span>$arr</span>[1]="two"<span>;
</span><span>$arr</span>[2]="three"<span>;

</span><span>$arr1</span>=<span>array</span><span>();
</span><span>$arr</span>[3]="taoshihan1"<span>;
</span><span>$arr</span>[4]="taoshihan2"<span>;
</span><span>$arr</span>[5]="taoshihan3"<span>;

</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => taoshihan1 [4] => taoshihan2 [5] => taoshihan3 )</span>

<span>$arr</span>=<span>array</span>("one","two","three"<span>);
</span><span>$arr1</span>=<span>array</span>("4","5","6"<span>);
</span><span>$newArr</span>=<span>array_merge</span>(<span>$arr</span>,<span>$arr1</span><span>);
</span><span>print_r</span>(<span>$newArr</span><span>);
</span><span>//</span><span>Array ( [0] => one [1] => two [2] => three [3] => 4 [4] => 5 [5] => 6 )</span>
Copy after login

Use the function ksort() to sort by key name. Note that no new array is returned, but the original array

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>ksort</span>(<span>$arr</span><span>);
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>Array ( [1] => taoshihan1 [2] => taoshihan2 [3] => taoshihan3 )</span>
Copy after login

Use the function array_search() to search for a certain key value and return the corresponding key

<span>$arr</span>=<span>array</span>("2"=>"taoshihan2","1"=>"taoshihan1","3"=>"taoshihan3"<span>);
</span><span>echo</span> <span>array_search</span>("taoshihan1",<span>$arr</span><span>);
</span><span>//</span><span> 1</span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126176.htmlTechArticle[PHP] Return to the basics (Array related functions), return to the array, use the function array_keys() to get the array All keys, parameters: array $arr = array (); $arr ['one']="one" ; $arr ['two']=...
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Three ways to get thread return value in Python Three ways to get thread return value in Python Apr 13, 2023 am 10:43 AM

When it comes to threads, your brain should have this impression: we can control when it starts, but we cannot control when it ends. So how do we get the return value of the thread? Today I will share some of my own practices. Method 1: Use a list of global variables to save the return value ret_values ​​= [] def thread_func(*args): ... value = ... ret_values.append(value) One reason to choose a list is: the append() of the list Methods are thread-safe, and in CPython, the GIL prevents concurrent access to them. If you use a custom data structure, and

How to solve the problem that scanf return value is ignored How to solve the problem that scanf return value is ignored Nov 14, 2023 am 10:01 AM

Solutions to the ignored return value of scanf include checking the return value of scanf, clearing the input buffer, and using fgets instead of scanf. Detailed introduction: 1. Check the return value of scanf. You should always check the return value of the scanf function. The return value of the scanf function is the number of successfully read parameters. If the return value is inconsistent with the expected one, it means that the input is incorrect; 2 , Clear the input buffer. When using the scanf function, if the input data does not match the expected format, the data in the input buffer will be lost, etc.

Use Java's Math.min() function to compare the size of two numbers and return the smaller value Use Java's Math.min() function to compare the size of two numbers and return the smaller value Jul 25, 2023 pm 01:21 PM

Use Java's Math.min() function to compare the sizes of two numerical values ​​and return the smaller value. When developing Java applications, sometimes we need to compare the sizes of two numerical values ​​and return the smaller number. Java provides the Math.min() function to implement this function. The Math.min() function is a static method of the JavaMath class. It is used to compare the size of two values ​​​​and return the smaller number. Its syntax is as follows: publicstaticintmi

Can a Golang function return multiple values? Can a Golang function return multiple values? Apr 13, 2024 pm 02:42 PM

Yes, Go functions can return multiple values ​​by returning a tuple, which is an immutable value that can contain different types of data.

C++ function pointer as function return value C++ function pointer as function return value Apr 14, 2024 am 08:30 AM

Function pointers can be used as function return values, allowing us to determine at runtime which function to call. The syntax is: returntype(*function_name)(param1,param2,...). Advantages include dynamic binding and a callback mechanism that allow us to adjust function calls as needed.

Common types of C++ function return value types Common types of C++ function return value types Apr 12, 2024 pm 05:36 PM

C++ function return types include: void (no return value), basic types (integers, floating point numbers, characters, and Boolean values), pointers, references, classes, and structures. When choosing, consider functionality, efficiency, and interface. For example, the factorial function that calculates factorial returns an integer type to meet functional requirements and avoid extra operations.

PHP returns the key name currently pointed to by the internal pointer of the array PHP returns the key name currently pointed to by the internal pointer of the array Mar 21, 2024 pm 04:21 PM

This article will explain in detail to you the key name currently pointed to by the internal pointer of the array returned by PHP. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the key name currently pointed to by the internal pointer of the array. PHP provides a function called key() to return the key name currently pointed to by the internal pointer of the array. This function works on indexed arrays and associative arrays. Syntax key(array) Parameter array: The array from which the key name is to be obtained. The return value is the key name currently pointed to by the internal pointer. If it is an index array, the integer index is returned; if it is an associative array, the string key name is returned. If the array is empty or the internal pointer points to the end of the array, NULL is returned.

How to use return value in Python How to use return value in Python Oct 07, 2023 am 11:10 AM

Python return value return usage is that when the function executes the return statement, execution will stop immediately and the specified value will be returned to the place where the function was called. Detailed usage: 1. Return a single value; 2. Return multiple values; 3. Return a null value; 4. End the execution of the function early.

See all articles