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:
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