Home Backend Development PHP Tutorial Summary of PHP common functions (array part) shared version will have some errors_PHP tutorial

Summary of PHP common functions (array part) shared version will have some errors_PHP tutorial

Jul 13, 2016 pm 05:47 PM
php and shared function Commonly used Summarize operate array Format generate use change enter output part mistake

Function name Purpose Format Input Output Operation

Array generation and conversion:

array() generates an array array array(mixed [...]) array value or, key => value an array variable None

array_combine() generates an array, using the value of one array as the key name and the value of the other array as the value array array_combine(array $keys, array $values) $keys is the array that provides the key, and $values ​​is the array that provides the value. Array None

range() creates an array of specified range units array range(mix $low,mix $high,[num $step]) $low minimum value, $high maximum value, $step step size Synthetic array None

compact() creates an array, including their variable names and values ​​arr compact(mix $varname,[,mix $...]) various variables, and the array returns an array consisting of variable names as keys and variable values ​​as values. It can also be a multi-dimensional array. It will be processed recursively without

array_fill() generates an array array with a given value array_fill(int $start, int $num, mix $value) Starting with the key of $start, $num elements, $value is the filling content Returns the completed array None

Array merging and splitting:

array_chuk() divides an array into multiple specified arrays array_chunk(arr $input,int $size[,bool $preserve_keys]) $input is the array to be operated on, $size contains several elements in each copy, $preserve_keys is Boolean, whether to preserve the keys The divided multi-dimensional array has no

array_merge() merges one or more arrays. If the keys of the elements are the same, the previous ones will be overwritten, and the sequence of indexes will not be overwritten, only arr will be automatically increased array_merge(arr $arr1[, arr $arr2,...] ) $arr1, etc. return the completed array for the operated array. None

Array comparison operations:

array_diff() Calculate the difference of arrays array array_diff(arr $arr1,arr $arr2[,...]) Input two or more arrays and compare the differences of several array element values ​​None

array_intersect() Calculate the intersection of arrays array array_intersect(arr $arr1,arr $arr2[,...]) Input two or more arrays Compare the intersection of several array element values ​​None

Array search and replace operation:

array_search() Search the given value from the array and return the key name mix array_search(mix $need,arr $arr[bool $stric]) $need is the search string, $arr is the target array, and $stric is true. If the matching variable type is successful, the key name will be returned. If it fails, it will return false. None

array_slice() Take out a segment arr array_slice(arr $array,int $offset[,int $length[bool $p_key]) from the array $array target array;$offset offset;$length interception length;$p_key:true Keep the key name. The key name $array target array will be reassigned by default; if $offset is non-negative, it will start from this offset. If it is a negative value, it will start from this far from the end; if $length is positive, there will be this in the sequence. Multi-unit, if not negative, the sequence ends at this far from the end. None

array_splice() Remove part of the array and replace arry with other values ​​array_splice(arr &$input,int $offset[,int $leng[,arr $rep]]) $input is the target array, $offset is the offset, $ If leng does not exist, all elements from $offset to the end will be removed. If it is a positive value, so many units will be removed. If leng is a negative value, so many units from $offk to the end of $leng will be removed. The replaced part will replace the original array. Note that the key names in the array are not retained after replacement. If $rep is given, the original array will be replaced by the new array elements

array_sum() calculates the sum of all values ​​in the array number array_sum(arr $array) target array returns the sum None

in_array() Finds whether a certain value exists in the array, case-sensitive bool in_array(mix $need,arr $array[,bool $str]) Searches $need in $array, if $str is true, the check type is also To match. Return true if found, false if not found None

array_key_exists() Check whether the given key name exists bool array_key_exists(mix $key, arr $serch) $key searches for the key, $serch target array returns whether it is found None

Array pointer operations:

key()

current() alias pos()

next()

prev()

end()

reset()

list() assigns the values ​​in the array to the variable void list(mix $var1[,$var2...])=arr $array

array_shift()

array_unshift()

array_push() The array finally pushes multiple elements int array_push(arr &$array,mix $var1[,$var2...]) &$array target array,$var1.. returns 1 for success when adding the value, and 0 for If failed, directly operate on the target array

array_pop() The last element of the array pops out mix array_pop(arr &$array) &$array is the target array, returns the value of the popped element, and directly operates on the target array

Array key value operations:

shuffle() shuffles the array and retains the key names bool shuffle(arr &$array) inputs the array and returns true. The key names will not change after shuffling the order

count()

array_flip() exchanges the keys and values ​​​​in the array arr array_flip(arr $trans) $trans is the operated array Returns the completed array None

array_keys() returns all the keys of the array to form an array arr array_keys(arr $input[,mix $search[,bool $str]]) If $search has a value, it will only return the key name corresponding to this value. If $str is true, , and also compare types to return an array composed of key names without

array_values() returns all the values ​​in the array to form an array array array_values ​​(array $input)

array_reverse() returns an array with the elements in the reverse order arr array_reverse(arr $input[,bool $key]) Input the target array, if $key is true, the original key name is retained. An array in the reverse order None

array_count_values() counts the number of occurrences of all values ​​in the array array array_count_values(array $input) $input is the array being operated on, using the value of input as the key and the number of occurrences as the value. None

array_rand() randomly extracts one or more elements from the array, note the key name!!! mix array_range(arr $input[,int $num]) $input is the target array, $num is the number of elements extracted and returned The key of the element, if it is one, it is a key name; if there are multiple elements, these keys will be used as values ​​to form an index array and return None

each()

array_unique() removes duplicate values ​​from the array, first sorts the values, then only retains the first encountered key name, and then ignores the remaining key names array array_unique(arr $array) Enter the target array key name and retain it Variable returns an array with no duplicate values ​​

Sort an array:

sort() sorts the array (from low to high), without retaining the key name bool sort(arr &$array[,int $flag]) The target array returns true if successful, false if failed. Rearranges the array elements and changes the keys at the same time. First name

rsort() sorts the array in reverse order (from high to low), without retaining the key name bool rsort(arr &$array[,int $flag]) The target array returns true if successful, false if failed. Rearranges the array elements and changes them at the same time. Key name

asort() Sort the array and keep the index relationship bool asort(arr &$array[,int $flag]) Target array Return true if successful, false if failed Sort the array and keep the original index or key

arsort() sorts the array in reverse order, maintaining the index relationship

ksort() Sorts the array by key name bool ksort(arr &$array[int $flag]) Target array Returns true if successful, false if failed Sorts the key name and retains the key-value correspondence

krsort() sorts the array in reverse order by key name

natsort() ""Natural algorithm""sorts the array, maintaining the key-value relationship" bool natsort(arr &$array) The target array returns true if successful, false if failed. Sorts the values ​​naturally, retaining the key-value relationship

natcasesort() natural sorting, case-insensitive bool natcasesort(arr &$array) target array returns true on success, false on failure, performs natural sorting of values ​​without case sensitivity, and maintains key-value correspondence

Excerpted from Battlefield Diary_LAMP Band of Brothers

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478505.htmlTechArticleFunction name purpose format input and output operation array generation and conversion: array() generates an array array array(mixed [. ..]) array value or, key = value an array variable without array_combine(...
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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles