Today, the editor will summarize some introductory learning notes for array operations in PHP, including: data creation, assignment, traversal, search, statistics, multi-dimensional arrays, etc. There are various array operations in PHP that you need to understand. Friends can refer to it.
What is an array?
An array is a collection of data, which is equivalent to a container. Data can be stored in this container according to certain rules. Equivalent to a hotel, there are many rooms in the hotel, and the rooms are numbered according to certain rules.
Array composition: The basic structure is as follows:
$array name (key) = value Array name: It is how one array is distinguished from another array, just like every hotel has a name.
Key: Also known as a pointer, index, or identifier. The key represents the location where a certain value is stored in the array, which is equivalent to the hotel's house number, and can be named in different ways. The corresponding value can be found by querying the key.
Value: The value is equivalent to what is stored in the room.
Assign value to create array
In PHP, there are two ways to create an array: variable assignment and function calling. Let’s talk about the former first.
Using the variable assignment method is very simple, just assign a value to an array variable directly.
php provides the array function to create an array. The basic structure is as follows: array (item1,item2... ,itemn)
/* item represents the element value in the array. The array() function automatically assigns identifiers to element values when creating an array, increasing from 0 */
Array key name
1. Key name assignment
When creating an array using the array() function, key names are automatically assigned to each value. In addition, we can also directly assign key names to elements according to our own needs.
Basic structural form:
array ( key => item )
Example 1:
When writing PHP programs, one-dimensional arrays sometimes cannot meet the needs. In this case, multi-dimensional arrays must be used. A multi-dimensional array is to add one or more subscripts to a one-dimensional array. The usage is roughly the same as that of a one-dimensional array, except that multi-dimensional data operations are more complex, but the function is more powerful.
Take a two-dimensional array as an example. It is like a small house inside a big house. The representation method is $a[0][0].
Outputting an array means displaying all element data of the array on the browser. How does PHP output an array? Commonly used PHP output array functions include var_dump() and print_r() functions.
1. The var_dump function recursively expands the array elements and displays the type, key name and element value of each element of the array.
foreach traverses the array
When we use arrays, we often need to traverse the array and obtain each key or element value. PHP provides some functions specifically for traversing arrays. Here we first introduce the usage of foreach array traversal function.
代码如下
复制代码
foreach( $color as $c) echo $c ." ";
改为:
foreach( $color as $key => $c) echo $key.$c ." ";
Structural form:
The code is as follows
Copy code
foreach (array_expression as $value) statement
/*array_expression is the array to be traversed
The function of as is to assign the value of the array to $value
Statement is the subsequent statement
*/
Example 1:
'white' ,
'black' => 'black' ,
'red' => 'red' ,
'green' => 'green',
'yellow' => 'yellow');
foreach( $color as $c) echo $c ." ";
?>
Not only the value of the element but also the key name can be obtained through foreach. The structural form:
foreach (array_expression as $key => $value) statement
Replace the code in line 7 in the above example:
The code is as follows
Copy code
foreach( $color as $c) echo $c ." ";
Change to:
foreach( $color as $key => $c) echo $key.$c ." ";
Find array element value
PHP can use array_search() to obtain the array key name. The structure is as follows:
array_search( $needle,$haystack )
/* The parameter $needle represents the value to be found */
/* $haystack represents the search object */
The array_search() function returns the key name, not the Boolean value, and returns false if it cannot be found. If the found element is exactly the first element, 0 is returned. PHP will automatically convert it to false, so you need to use "===" to determine the return value. ("===" determines whether they are congruent, details: PHP relational operators)
$s=array("a","b","c","d","e","f");
$i=array_search("a",$s); /* Find whether the array contains the character "a" */
if($i===false) /* Determine the search results */
echo "Character 'a' not found in array s";
else echo "Output the key name of array $s:" .$i; /* Output key name */
Arrays can also be operated like variables. For example, when we need PHP to count the number of array elements, we can use the count() function to calculate the number of elements in the array.
Structural form:
The code is as follows
Copy code
count ( $var, $mode )
/* $var parameter $var is usually an array, and the function returns the number of cells in var */
That is, the order is based on the size of the identifier ASCⅡ code value.
ksort(): Sort by array identifier order
krsort(): Sort array identifier in reverse order
Example 1:
Sort by element value
asort(): Sort the array from small to large;
rsort(): Sort the array in reverse order from large to small.
Change lines 8-11 of Example 1 to:
TRUE if $a and $b have the same key/value pairs and are of the same order and type.
$a != $b
Not equal
TRUE if $a is not equal to $b.
$a <> $b
Not equal
TRUE if $a is not equal to $b.
$a !== $b
Not congruent
TRUE if $a is not exactly equal to $b.
http://www.bkjia.com/PHPjc/628836.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628836.htmlTechArticleThe editor will summarize some introductory learning notes for array operations in PHP today, including: data creation, Assignment, traversal, search, statistics, multi-dimensional arrays, etc. various array operations in php...
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
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
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
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
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,
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
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
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 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.