The content introduced in this article is about PHP arrays, which has certain reference value. Now I share it with you. Friends in need can refer to it
Array
Declaration and use of array
Comparison
Scalar: a standard quantity that only stores one value
Array: A combination of data, which can store multiple values
Category
Index array
Associative array
Declaration
Direct declaration
$array=array(1,2,3,4); Index array
$array=array('name'=>'zhansan','sex'=>'nan');Associative array
Declare index array
$array[0]=1
Declare associative array
$array['name']='zhangsan'
Key (key)
Subscript: The corresponding representation in the data is the 0 and name written above
Value (value)
Data corresponding to the subscript in the array
Element
The combination of key and value is collectively called an element
Use
Array name [subscript]
Features
1. You can use [ ] to declare and operate an array or { } to Declare and operate arrays, but it is recommended to use [ ]
2. When declaring an array, you can only use [ ] without adding a subscript. This will default to an index array, and the subscript starts from zero and increases in sequence
3. When [ ] is used alone without specifying a subscript, the value of the index subscript will be the largest subscript 1
4. When declaring an array, The table under the index array can be discontinuous
5. The subscript of the array can be negative, but it does not make much sense. It is not recommended to use
6. If the subscript has In the case of repetition, the later one will overwrite the previous one (the index and association are the same)
7. If you declare the associative array without quotation marks to include the subscript, it will be regarded as Constants are used to process
8. Associative arrays and index arrays can exist in the same array at the same time. The mixed array is an associative array
9. Under the association of arrays The subscript will not affect the arrangement rules of the index subscript
Delete the elements in the array
unset() directly releases it in the memory
Reset The assignment is null and the value is set to empty
Note
After using unset to release the elements in the array, the subscript will also be deleted. When the array elements are declared again, If you don't write the subscript, it will change from the maximum value 1
Multi-dimensional array
dimension, that is, put the array inside the array (it can be an associative array or an index Array)
Array traversal
for
Only the index array can be traversed, and the subscripts of the array must be continuous.
foreach
Format
foreach (array as Customize a variable to store the subscript => Customize a value to use (for storing values) {}
Note
How many elements there are in the array, traverse as many times as possible (automatic)
No matter how many elements there are in the array Is it an index array or an associative array
Pointer function
key()
Get the key of the current element
current()
Get the value of the current element
next()
Move the pointer down and return the next pointer When the corresponding value reaches the end, it returns false
prev()
Move the pointer upward and return the value corresponding to the previous pointer
end()
Move to the end and return the value corresponding to the element
reset()
Move to the first one and return the value corresponding to the element
Super global array
Instructions
Can be used anywhere
$_GET
Receive the http protocol and pass it in the get request method The passed value $_GET can obtain the value submitted by the form get method, the parameters in the a connection, and the parameters in the action of the form can be obtained
$_POST
Receive The value passed by the http protocol in the post request method is generally submitted using the post method
$_REQUEST
Receives $_GET and $_POST and The data passed by $_COOKIE is not recommended to be used
$_COOKIE
Receives an array of variables passed to the script through the httpcookie method
$_SESSION
Array of SESSION variables available in the current script
$_FILES
Receive file upload information
$_GLOBALS
Receive all variables in the script
$_SERVER
Some variables about the server
HTTP_HOST User's host
HTTP_USER_AGENT What browser does the user use to access
SERVER_NAME Server name
SERVER_ADDR Server IP address
REMOTE_ADDR The IP address of the user browsing the current script
DOCUMENT_ROOT root directory address
SERVER_PORT port number
SCRIPT_NAME The path of the current script
Function
How to learn the function
Know what this parameter is used for
What are the parameters
What type is the return value
Pseudo-types in the parameters
mixed It means that one parameter can receive multiple types of Different types
number indicates that a parameter is int or float
callback callback function
void If used as a return type, the return value is useless , as a parameter indicates that this function does not accept any parameters
Note [ ] indicates optional parameters
The return value of the function
Return What type is the value?
Stack and Queue
Stack
The process of pressing bullets is first in, last out. We usually call it a stack. Pushing data is called pushing, and popping data is called popping. Features, first in, last out
Queue
The process of queuing at the station, first in first out
Declaration and use of arrays
Comparison
Scalar: a standard quantity that only stores one value
Array: a combination of data, which can store multiple values
Classification
Index array
Associative array
Declaration
Direct declaration
$array=array(1,2,3,4); Index array
$array=array('name'=>'zhansan','sex'=>'nan ');Associative array
Declare index array
$array[0]=1
Declare associative array
$array['name']='zhangsan'
Key
Subscript: The corresponding representation in the data is the 0 and name written above
Value (value)
The data corresponding to the subscript in the array
Element
The combination of key and value is collectively called an element
Use
Array name [subscript]
Features
1. You can use [ ] to declare and operate arrays. You can also use { } to declare and operate arrays, but it is recommended to use [ ]
2. When declaring an array, you can use [ ] without adding a subscript. This will default to an index array. The subscript starts from zero and increases in sequence
3. Use [ ] alone. , when the subscript is not specified, the value of the index subscript will be the largest subscript 1
4. When declaring an array, the table below the index array can be discontinuous
5. The subscript of the array can be a negative number, but it does not make much sense. It is not recommended to use
6. If there are repeated subscripts, the latter one will overwrite the previous one ( Indexes and associations are the same)
7. If you declare an associative array without quotation marks to include the subscript, it will be treated as a constant
8. Associative arrays and index arrays can exist in the same array at the same time. The mixed array is an associative array
9. The associated subscripts of the array will not affect the arrangement rules of the index subscripts
Delete elements in the array
unset() releases directly in memory
Reassign the value to null and set the value to empty
Note
After using unset to release the elements in the array, the subscript will also be deleted. When the array element is declared again, if the subscript is not written down, it will change from the maximum value 1
Multidimensional array
The dimension is to put the array inside the array (it can be an associative array or an index array)
Array traversal
for
Only the index array can be traversed, and the subscripts of the array must be consecutive.
foreach
Format
foreach (array as Customize a variable to store the subscript => Customize a value to use (for storing values) {}
Note
How many elements there are in the array, traverse as many times as possible (automatic)
No matter how many elements there are in the array Is it an index array or an associative array
Pointer function
key()
Get the key of the current element
current()
Get the value of the current element
next()
Move the pointer downward and return the value corresponding to the next pointer to the end Returns a false
prev()
The pointer moves upward and returns the value corresponding to the previous pointer
end()
Move to the end and return the value corresponding to the element
reset()
Move to the first and return the value of the element Corresponding value
Super global array
Description
Can be used anywhere
$_GET
Receive the value passed by the http protocol in the get request method $_GET can get the value submitted in the form get method, the parameters in the a connection, and the parameters in the action of the form can be obtained
$_POST
Receive the value passed by the http protocol in the post request method. When submitting a form, you usually use the post method to submit
$_REQUEST
Receive the data passed by $_GET, $_POST and $_COOKIE It is not recommended to use
$_COOKIE
Receive the variables passed to the script by httpcookie method Array of
$_SESSION
Array of SESSION variables available in the current script
$_FILES
Receive file upload information
$_GLOBALS
Receive all variables in the script
$_SERVER
Some variables about the server
HTTP_HOST User's host
HTTP_USER_AGENT What browser does the user use to access
SERVER_NAME Server name
SERVER_ADDR Server IP address
REMOTE_ADDR The IP address of the user browsing the current script
The address of the DOCUMENT_ROOT root directory
SERVER_PORT Port number
SCRIPT_NAME Path of the current script
Function
How to learn the function
Know what this parameter is used for
What are the parameters
What type is the return value
Pseudo-types in parameters
mixed Indicates that a parameter can receive multiple different types
number Indicates that a parameter is int or float
callback callback function
void If as The return type indicates that the return value is useless. As a parameter, it indicates that this function does not accept any parameters
Note [ ] indicates optional parameters
The return value of the function
What type is the return value?
Stack and queue
Stack
The process of pressing the bullet is first in, last out. We usually call it a stack. Pushing data is called pushing, and popping data is called popping. Features, first in, last out
Queue
The process of queuing at the station, first in, first out
Related recommendations:
PHP's array definition and traversal details
The above is the detailed content of php array. For more information, please follow other related articles on the PHP Chinese website!