Introduction to PHP array arrays and array functions Daquan _php tutorial

Source: Internet
Author: User
PHP Tutorial Array Overview and Arrays function Daquan
The array function allows you to manipulate the array.

PHP supports single-dimension and multidimensional arrays. It also provides a function to construct an array using the Database tutorial query results.
Installation
The array function is part of the PHP core. These functions can be used without installation.
PHP Array function
PHP: Indicates the earliest version of PHP that supports this function.

An array of PHP is actually an associative array, or a hash table. PHP does not need to pre-declare the size of the array, it can be directly assigned to create the array. For example:
The most traditional, with the number of keys, assign value
$state [0]= "Beijing";
$state [1]= "Hebei";
$state [2]= "Tianjin";
If the key is an incremented number, you can omit the
$city []= "Shanghai";
$city []= "Tianjin";
$city []= "Guangzhou";
Make a key with a string
$capital ["China"]= "Beijing";
$capital ["Japan"]= "Tokyo";
It is more convenient to use array () to create an array, you can pass an array element as a parameter to a set, or you can create an associative array with the-= operator. For example:
$p =array (1,3,5,7);
$capital =array ("China" = "Beijing", "Japan=>" Tokyo ");
Array is actually a syntactic structure, not a function. Like an array, there is also a list (), which can be used to extract values from an array and assign values to multiple variables. For example:
List ($s, $t) = $city;
Echo $s, ", $t;
Output result: Shanghai Tianjin
Note that the list method can only be used for arrays that are indexed by numbers.
PHP has built a number of commonly used array processing functions, in particular, refer to the manual. Examples of commonly used functions are as follows: Count or sizeof can get the length of the array, Array_merge can merge two, or multiple arrays, Array_push (POP) can use the array like a stack.
Copy the code code as follows:
$state [0]= "Beijing";
$state [1]= "Hebei";
$state [2]= "Tianjin";
$city []= "Shanghai";
$city []= "Tianjin";
$city []= "Guangzhou";
$capital ["China"]= "Beijing";
$capital ["Japan"]= "Tokyo";
echo Count ($city), '
';
Array_push ($capital, "Paris");
$newarray =array_merge ($city, $capital);
foreach ($newarray as $elem)
echo $elem. '
';
?>

The output is:
3
Shanghai
Tianjin
Guangzhou
Beijing
Tokyo
Paris


P

Function Description PHP
Array () to create arrays. 3
Array_change_key_case () returns an array whose keys are both uppercase and lowercase. 4
Array_chunk () splits an array into a new array block. 4
Array_combine () Creates a new array by merging two arrays. 5
Array_count_values () is used to count the occurrences of all values in the array. 4
Array_diff () returns an array of difference sets for two arrays. 4
ARRAY_DIFF_ASSOC () compares key names and key values, and returns an array of two arrays of differences. 4
Array_diff_key () compares the key name and returns an array of difference sets for two arrays. 5
ARRAY_DIFF_UASSOC () computes the difference set of the array by indexing the user-supplied callback function. 5
Array_diff_ukey () computes the difference set of the array by using the callback function to compare the key names. 5
Array_fill () fills the array with the given value. 4
Array_filter () filters the elements in the array with a callback function. 4
Array_flip () Swaps the keys and values in the array. 4
Array_intersect () computes the intersection of the array. 4
ARRAY_INTERSECT_ASSOC () compares the key name and key value, and returns an array of two arrays of intersections. 4
Array_intersect_key () computes the intersection of an array using the key name comparison. 5
The ARRAY_INTERSECT_UASSOC () index checks the intersection of the computed array, using the callback function to compare the indexes. 5
Array_intersect_ukey () computes the intersection of the array using the callback function to compare the key names. 5
Array_key_exists () checks whether the given key name or index exists in the array. 4
Array_keys () returns all the key names in the array. 4
Array_map () functions The callback function on the cell of the given array. 4
Array_merge () merges one or more arrays into an array. 4
Array_merge_recursive () recursively merges one or more arrays. 4
Array_multisort () sorts multiple arrays or multidimensional arrays. 4
Array_pad () fills the array to the specified length with a value. 4
Array_pop () pops the last element of the array (out of the stack). 4
Array_product () computes the product of all the values in the array. 5
Array_push () presses one or more cells (elements) into the end of the array (into the stack). 4
Array_rand () randomly selects one or more elements from the array and returns them. 4
Array_reduce () iteratively simplifies the array to a single value using a callback function. 4
Array_reverse () flips the order of elements in the original array, creates a new array, and returns. 4
Array_search () searches the array for the given value and returns the corresponding key name if successful. 4
Array_shift () deletes the first element in the array and returns the value of the deleted element. 4
Array_slice () takes a value out of the array and returns it as a condition. 4
Array_splice () removes part of the array and replaces it with other values. 4
Array_sum () computes the and of all values in the array. 4
Array_udiff () compares the data with the callback function to calculate the difference set of the array. 5
ARRAY_UDIFF_ASSOC () with index check computes the difference set of the array, using the callback function to compare the data. 5
ARRAY_UDIFF_UASSOC () takes an index check to calculate the difference set of the array, using the callback function to compare the data and index. 5
Array_uintersect () computes the intersection of the array, using the callback function to compare the data. 5
The ARRAY_UINTERSECT_ASSOC () index checks the intersection of the computed array and compares the data with the callback function. 5
The ARRAY_UINTERSECT_UASSOC () index checks the intersection of the computed array, using the callback function to compare the data and the index. 5
Array_unique () deletes duplicate values in the array. 4
Array_unshift () inserts one or more elements at the beginning of the array. 4
Array_values () returns all the values in the array. 4
Array_walk () applies a user function to each member in the array. 3
Array_walk_recursive () applies the user function recursively to each member in the array. 5
Arsort () Reverses the array and maintains the index relationship. 3
Asort () sorts the array and maintains the index relationship. 3
The compact () establishes an array, including the variable names and their values. 4
Count () computes the number of elements in the array or the number of attributes in the object. 3
Current () returns the element in the array. 3
Each () returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () points the inner pointer of the array to the last element. 3
Extract () Imports variables from the array into the current symbol table. 3
In_array () checks whether the specified value exists in the array. 4
Key () Gets the key name from the associative array. 3
The Krsort () array is reversed by key name. 3
Ksort () The array is sorted by key name. 3
List () assigns the values in the array to some variables. 3
Natcasesort () uses the "natural sort" algorithm to sort an array of case-insensitive letters. 4
Natsort () sorts the array with the "natural sort" algorithm. 4
Next () Moves the inner pointer in the array forward one bit. 3
The alias of the POS () current (). 3
Prev () returns the internal pointer of the array back to one bit. 3
Range () creates an array of elements that contain the specified range. 3
Reset () points the inner pointer of the array to the first element. 3
Rsort () Reverses the order of the arrays. 3
Shuffle () rearranges the elements in the array in random order. 3
The alias of sizeof () count (). 3
Sort () sorts the array. 3
Uasort () Sorts the values in the array using a user-defined comparison function and keeps the index associated. 3
Uksort () Sorts the key names in the array using a user-defined comparison function. 3
Usort () Sorts the values in the array using a user-defined comparison function. 3
PHP Array Constants
PHP: Indicates the earliest version of PHP that supports this constant.

Constants Description PHP
Case_lower is used in Array_change_key_case () to convert the array key names to lowercase letters.
Case_upper converts the array key name to uppercase in Array_change_key_case ().
SORT_ASC is used in the Array_multisort () function to keep it in ascending order.
Sort_desc is used in the Array_multisort () function to keep it in descending order.
The sort_regular is used to perform a common comparison of objects.
The sort_numeric is used to compare objects numerically.
Sort_string is used for string comparisons of objects.
Sort_locale_string A string comparison of objects based on the current region. 4
Count_normal
Count_recursive
Extr_overwrite
Extr_skip
Extr_prefix_same
Extr_prefix_all
Extr_prefix_invalid
Extr_prefix_if_exists
Extr_if_exists
Extr_refs


http://www.bkjia.com/PHPjc/445370.html www.bkjia.com true http://www.bkjia.com/PHPjc/445370.html techarticle PHP Tutorial Array array Introduction and array function Daquan The array function allows you to manipulate the group. PHP supports single-dimension and multidimensional arrays. also provides the database tutorial query results to construct ...

  • 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.