PHP Basic Syntax 4

Source: Internet
Author: User

1. Arrays

An array is a compound type that is stored in an in-memory heap, which can hold multiple different types of data, and the length defaults to No limit!

The composition of the array

An array, which consists of elements, each of which is a member of the array!

The name of the element, called the Subscript (index), also called the Key (key), the value of the element, called the value (value)

Therefore, an element in an array is also called a "key-value pair" (Key/value)

Create an array to create an explicit

is to create with an array explicitly:

Implicitly-Created

It is actually created using the square brackets syntax of the array:

Where the key name can default, the system will automatically add index subscript!

2. Pointers to arrays

Within each array, there is a pointer to the elements of an element (invisible), which is responsible for pointing to an element within the array, so the pointed element is called the "current element"!

Typically, if we define an array and then do nothing, the pointer to the array points to the first element of the array!

Pointer-related functions:

Key (): The syntax is: Key (array name), function: Gets the key of the current element of the array! Current (): The syntax is: the present (array name), function: Gets the value of this array's element! Next (): The syntax is: Next (array name), action: Move the pointer of the array down one reset (): The syntax is: RESET (array name), function: Resets the pointer to the array (the pointer to the array is redirected to the first element) prev (): Moves the array pointer up, Previous (previous) end (): Reset moves the pointer of the array to the first element, then end is the reset pointer, but resets to the last element! 3, the array of common functions each (): At the same time the function of key, current, next function, you can get the value of the present element, you can also get the key of the current element!

Note the return value of each:

1), returns an array, and is a mixed array of indexes and associations

2), a new array is recreated based on the current element of the original array, where the values and keys of the original array elements are the values of the new array, and the index subscript and the associated subscript are created for each of them.

where indexed:$each [0] represents the key of the current element of the original array, $each [1] represents the value of the current element of the original array

The associated:$each [' key '] represents the key of the current element of the original array, $each [' value '] represents the value of the current element of the original array

List (): is also a system function, it can use an indexed array, one-time to assign values to multiple variables! While+each+list Traversal:

COUNT (): Calculates and returns the number of elements in the array or the number of attributes in the Object! In addition, Count has a second parameter, which defaults to 0 or false, and if it is 1 or true, recursive statistics are supported (the values of the internal array are also counted) range (): Use range to define an array with three parameters: 1), start character or number, 2), end character or number 3), Pace: Jump a few characters or numbers at a time Array_merge (): Array_merge () Merges the cells of one or more arrays, and the values in an array are appended to the previous array, returning the array as the result. If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original value, but append to the back (re-index). Indexes are re-indexed even if the index subscript does not conflict. If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential way. Alternatively, the operator "+" can also implement the merge of the array, the rule: whether it is an indexed array or an associative array, it will discard the elements of the following repeating subscript directly! Array_rand (): Here rand is random meaning, the function is to randomly extract the keys of several elements within the array! and returns an indexed array! Requires two parameters: the first one is an array name, the second is the number of shuffle (): The function is to disrupt an array, and is directly on the original array to disrupt! And it will regenerate the index subscript! Max () and Min (): You can also get the maximum and minimum values in the array! Array_keys (): Gets the key of all elements, returns an indexed array! Array_values (): Gets the value of all elements and returns an indexed array!array_combine ():There are two parameters, all arrays, take the value of the first array as the key of the new array, and the value of the second array is re-assembled into a new array as the value of the new array!array_flip ():Swaps the keys and values of an array!In_array ():Determine if a data is the value of an element inside an array! Returns a Boolean value! Requires two parameters: the first is a specific data, the second is an array name array_key_exists (): Determine if there is a key in an array, also return a Boolean value, also requires two parameters: the first is the specific data, the second is an array name Array_ Search ():function isSearches for the value of an element within an array and returns its subscript value! Requires at least two parameters: the first parameter is a specific value, the second argument is an array name Array_chunk (): Splits an array into multiple arrays, where the number of cells per array is determined by the second parameter. The number of cells in the last array may be less than the second parameter. Implode ():function isConcatenate all the values of an array element into a string using a specified delimiter! Requires two parameters: the first is a separator, here can be omitted, if omitted, that is no delimiter, the second is an array name; explode ():function isA string, using the delimiter in it, splits it into multiple parts to form an array! Requires two parameters: the first parameter is a delimiter, the second argument is a string! Extract (): Converts each element in the array into a variable whose name is the key to the array element, and the value of the variable is the value of the array element! It is common to put some data into an array, then pass it as a function parameter into a function, and then extract the array out of the function, resulting in the required variables! Compact (): Using multiple variables to form an array, it is clear that the key to the array element is the name of the variable, and the value of the array element is the value of the variable! Usage Scenario: If there are many return values for a function, you can put all the return values in an array and return to the array! Array_map (): There are two parameters, the first one is a function, the second is an array or an array list, so that each cell in the subsequent array is processed by the first function, returning the processed arrayArray_push ():Press one or more data into the tail of the array!Array_pop ():POPs the last data of the arrayArray_unshift ():Add data from the front of the arrayArray_shift ():Pop the data from the front of the array sort (): Sort the elements in a group in ascending order, that is, from low to high! After sorting, the keys of the original array are lost and an indexed array is regenerated! Asort (): is also sorted in ascending order, but the previous key-value pair will be preserved after sorting! Rsort (): R here is the meaning of reverse! The values of the array elements are sorted in descending order, that is, from high to Low: an indexed array is regenerated after sorting! Arsort (): is also a descending sort, just preserves the previous key-value pairs!

PHP Basic Syntax 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.