PHP array function knowledge summary_php skills
This article shares the basic knowledge of PHP array functions for your reference. The specific content is as follows
Array array is a very important data type. Compared to other data types, it is more like a structure, and this structure can store a series of values. Arrays can store many values in a single variable name, and a value can be accessed by referencing a subscript.
In PHP, there are three array types:
indexed array - array with numeric index
Associative array - array with specified keys
Multidimensional array - an array containing one or more arrays
1. Create an array
array(key => value)
1. Create an index array
Use the array() function to declare an array. PHP is a weakly typed language that is relatively flexible and convenient. It can also be the element value of the array directly. There is no need for a key value. The index is automatically assigned (the index starts from 0).
example:
1 2 3 4 5 6 7 |
|
2. Create an associative array
Associative arrays are similar to index arrays, except that associative arrays cannot only be numbers like the key names of index arrays. They can be numerical values, strings, and mixed forms. The basis for determining whether an array is an associative array is: Array Whether there is a key name in that is not a number. No, it's related.
array("Robin Li" => "Baidu","Jack Ma" => "Alibaba","Ma Huateng" => "Tencent");
3. Multidimensional array
array(array(),array()) two-dimensional array
Get the length of the array - count() function
1 2 3 4 |
|
2. Traverse the array
Output the value of the element in the array. For index arrays, for and foreach are commonly used; for associative arrays, foreach is commonly used. Use the print_r() function to print the results after the loop, or var_dump().
1. Traverse the index array
Traverse and output all values of the index array, you can use for, foreach(array_expression as value), foreach(arrayexpressionaskey => $value) loop, as follows:
Use for loop
1 2 3 4 5 6 7 8 |
|
The print result is displayed as follows:
Array
(
[0] => Baidu
[1] => Ali
[2] => Tencent
)
Using foreach loop
1 2 3 4 5 6 7 |
|
Note: There is an array symbol [] after data. Why?
2. Traverse associative array
Traverse and output all values of the associative array, you can use the foreach (array_expression as key=>value) loop, as follows:
1 2 3 4 5 6 7 |
|
The print result shows:
1 2 3 4 5 6 |
|
Did you notice? At this time, what is after data is [$key]? instead of []
One is a numeric associative array and the other is a numeric index array,
3. Add and delete elements of the array
Add
to the end of the array element
The array_push(array,value1,value2…) function adds one or more elements (push) to the end of the array of the first parameter and returns the length of the new array.
This function is equivalent to multiple calls to array[]=value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Add at the beginning of the array element
The array_unshift(array,value1,value2,value3…) function is used to insert new elements into the array. The values of the new array will be inserted at the beginning of the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Delete at the end of the array element
The array_pop(array) function removes the last element from the array.
1 2 3 4 5 6 7 8 9 10 11 |
|
Delete at the beginning of the array element
The array_shift(array) function deletes the first element in the array and returns the value of the deleted element.
1 2 3 4 5 6 7 8 9 10 11 |
|
Remove duplicate values from the array
The array_unique(array) function removes duplicate values from an array and returns the resulting array.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
4. Positioning array elements
Search for values present in the array
in_array(search,array,type) checks whether the specified value exists in the array.
Returns true if the given value search exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value. If the parameter is not found in the array, the function returns false.
1 2 3 4 5 6 |
|
Retrieve a value from the array based on conditions: array_slice(array,start,length,preserve)
start is required. numerical value. Specifies the starting position of the element to be retrieved. 0 = first element.
If the value is set to a positive number, it will be taken from front to back.
If the value is set to a negative number, the absolute value of start is taken from back to front. -2 means start from the second to last element of the array.
length Optional. numerical value. Specifies the length of the returned array.
If the value is set to an integer, that number of elements is returned.
If this value is set to a negative number, the function will terminate fetching this far from the end of the example array.
If this value is not set, all elements starting from the position set by the start parameter to the end of the array are returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
array_splice(array,start,length,array) function removes the selected element from an array and replaces it with a new element. This function will also return the array containing the removed elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
5. Array merging, splitting and comparison
The array_merge() function merges arrays together and returns a combined array. The resulting array starts with the first input array parameter and is appended in the order in which subsequent array parameters appear.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Append array recursively
The array_merge_recursive() function is the same as array_merge() and can merge two or more arrays together to form a combined array. The difference between the two is that the function takes a different approach when a key in an input array is already present in the result array. array_merge() overwrites the previously existing key/value pairs and replaces them with the key/value pairs in the current input array, while array_merge_recursive() merges the two values together to form a new array with the original keys as an array name. Its form is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Merge two arrays
The array_combine() function will generate a new array, which consists of a set of submitted keys and corresponding values, in the form:
1 2 3 4 5 6 7 8 9 10 11 |
|
Find the intersection of arrays
The array_intersect() function returns a key-preserved array consisting only of values that appear in the first array and appear in every other input array. Its form is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Note: array_intersect() will consider two elements equal only if they have the same data type.
Find the intersection of associative arrays
array_intersect_assoc() is basically the same as array_intersect(), except that it also considers the keys of the array in the comparison. Therefore, only key/value pairs that appear in the first array and also appear in all other input arrays are returned in the result array. Its form is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Find the difference set of associative arrays
The function array_diff_assoc() is basically the same as array_diff(), except that it also takes into account the keys of the arrays when comparing, so key/value pairs that only appear in the first array and not in other input arrays will be returned. in the result array. Its form is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
|
Other useful array functions
Returns a random set of keys The array_rand() function will return one or more keys in an array. Its form is:
1 2 3 4 5 6 7 8 9 10 |
|
Sum the values in the array
The array_sum() function adds all the values in the array together and returns the final sum, which has the following form:
1 2 3 4 |
|
The print result shows:
44
If the array contains other data types (such as strings), these values will be ignored.
Divide array
The array_chunk() function decomposes the array into a multi-dimensional array, which consists of multiple arrays containing size elements. Its form is as follows:
1 2 3 4 |
|
The print result shows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The functions that can be called when processing arrays are
1 2 3 4 5 6 |
|
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
