A more comprehensive summary of the use of PHP arrays

  • 2020-03-31 21:16:21
  • OfStack

What is an array
An array is a collection of data, a series of data that is organized into an actionable whole. Each entity in the array contains two items: a key and a value.
Ii. Declaration of data
There are two main ways to declare arrays in PHP:
One is to declare an array using the array() function,
One is to directly assign values to array elements. Feifei Asp! Technology park
< 1 > Array () the way the array() function declares an array([mixed...]) ) the syntax of the parameter mixed is key=> The value
Such as,

< ? PHP
$array = array (" 1 "= >" ", "2" = >" Cheng ", "3" = >" Word ", "4" = >" Canon ");
Print_r ($array);
Echo "< Br>" ;
Echo $array [1]. // note: the subscript starts at 0 by default
Echo $array [2].
Echo $array [3].
Echo $array [4].
? >

< 2 > Directly assign values to array elements.
This is a good way to create an array if you do not know the size of the array you are creating, or if the size of the array may change when you actually write the program.
Such as,

< ? PHP
$array [1] = "I";
$array [2] = "love";
$array [3] = "PHP";
Print_r ($array); // outputs the structure of the created array
? >

Type of array
PHP supports two kinds of arrays: indexed array, which USES Numbers as keys, and associative array, which USES strings as keys.
Output array
In PHP, the output of array elements can be achieved through echo and print statements, but this can only output an element in the array; To output the array structure, use the print_r() function, whose syntax is print_r(mixed expression), and output the variable itself if the parameter expression is a normal integer, character, or real variable.
Five, the construction of array
One-dimensional array:
An array is called a one-dimensional array when its elements are variables. As. P technology park
Declared one-bit array: type descriptor array name [constant expression];
Two-dimensional array:
When the elements of an array are one-digit arrays, they are called two-dimensional arrays.
Such as,

< ? PHP
$STR = array (
"Network programming language "=> Array (" PHP ", "the JSP", "ASP"),
"Sports "=> Array (" m "= >" Football, "" n" = >" Basketball "));
Print_r ($STR);
? >

Go through the groups
Traversing all the elements in a group is a common operation that allows queries or other functions to be performed during traversal. There are many ways to iterate over groups of Numbers in PHP, and here are two of the most common.
< 1 > Use the foreach structure to iterate over the groups;
< 2 > The list() function can only be used for arrays with a numeric index, and the numeric index starts at 0.
Example: using a combination of list() and each() to authenticate user login:

< ? PHP
// output user login information
While (the list ($name, $value) = each ($_POST)) {
If ($name! = "submit") {
Echo "$name = $value< Br>" ;
}
}
? >

Count the number of array elements
In PHP, the count() function is used to count the number of elements in an array. The syntax is int coun(mixed array[,int mode]), where the parameter array is required and the mode is optional. Such as,

< ? PHP
$array = array (" PHP "= > Array ("PHP function reference ","PHP program development sample ","PHP database system development complete manual "),
"Asp" = > Array ("ASP tips and tricks ")
); // declares a two-dimensional array
Echo the count ($array, COUNT_RECURSIVE); // recursively count the number of elements in the array, and the result is 6
? >

Array sort
< 1 > Sort () and rsort() are used to ascending and descending arrays respectively, such as,
< ? PHP
$array = array (5,26,37,18,9,42,88,66);
$array1 = sort ($array);
For ($I = 0; $i< Count ($array); ${i++)
Echo $array [$I]. "";
}
Echo "< Br>" ;
$array1 = rsort ($array);
For ($I = 0; $i< Count ($array); ${i++)
Echo $array [$I]. "";
}
? >
Operation results:
5, 9, 18, 26, 37, 42, 66, 88
88, 66, 42, 37, 26, 18, 9, 5
< 2 > Sort the associative array using ksort() and asort()
Ksort () and asort() functions are needed if you use a correlation array and you want to keep the keyword and value sorted the same after sorting
The number, such as,
< ? PHP
$array = array (' PHP '= > 1, 'JSP' = > 2, 'asp = > 3);
The ksort ($array);
Print_r ($array);
Echo "< Br>" ;
Asort ($array);
Print_r ($array);
? >
Operation results:
Array ([asp] = > 3 / JSP = > 2 [PHP] = > 1)
Array ([PHP] = > 1 / JSP = > 2 / asp = > 3)

Related articles: