Examples of array arrays in PHP

Source: Internet
Author: User
Tags sorts
The array function in PHP allows you to access and manipulate arrays. Supports simple arrays and multidimensional arrays. The PHP Array function is part of the PHP core. These functions can be used without installation.

Definition and usage

The array () function is used to create arrays. In PHP, there are three types of arrays: Valarray-array associative array with numeric ID key-an array with the specified key, each key associated with a value, multidimensional array-syntax for array syntax arrays with one or more arrays of numbers: Array (value1,value2,value3,et C.); Syntax for associative arrays: Array (key=>value,key=>value,key=>value,etc.); The parameter description keys specify the key name (numeric or string). value specifies the key value. Technical details return value: Returns an array of parameters. PHP Version: 4+ update log: Since PHP 5.4, you can use the short array syntax with [] instead of array (). For example, use $cars =["Volvo", "BMW"); Instead of $cars =array ("Volvo", "BMW"); Create an associative array named $age:

<?php$age=array ("Peter" and "+", "Ben" = "Panax Notoginseng", "Joe" and "a"); echo "Peter is". $age [' Peter ']. "Years old.";? >

To traverse and print the value of an array of values:

<?php$cars=array ("Volvo", "BMW", "Toyota"), $arrlength =count ($cars), for ($x =0; $x < $arrlength; $x + +) {echo $cars [ $x];echo "<br>";}? >

To traverse and print the values of an associative array:

<?php$age=array ("Peter" and "+", "Ben" = "Panax Notoginseng", "Joe" = "+"), foreach ($age as $x = + $x _value) {echo "key=". $ X. ", value=." $x _value;echo "<br>";}? >

To create a multidimensional array:

<?php//a two-dimensional array $cars=array ("Volvo", 100,96), Array ("BMW", 60,59), Array ("Toyota", 110,100)); >
function Description
Array () Creates an array.
Array_change_key_case () Returns an array whose keys are uppercase or lowercase.
Array_chunk () Splits an array into a new array block.
Array_column () Returns the value of a single column in the input array.
Array_combine () Create a new array by merging two arrays (one for the key an array group and one for the key-value array).
Array_count_values () Used to count the occurrences of all values in the array.
Array_diff () Compares an array and returns the difference set of two arrays (only the key values are compared).
ARRAY_DIFF_ASSOC () Compares an array and returns the difference set (comparison key name and key value) of two arrays.
Array_diff_key () Compares an array and returns the difference set of two arrays (only the key names are compared).
ARRAY_DIFF_UASSOC () Compares arrays, returns the difference set of two arrays (comparison key names and key values, using a user-defined key-name comparison function).
Array_diff_ukey () Compares arrays, returns the difference set of two arrays (only the key names are compared, using the user-defined key-name comparison function).
Array_fill () Fills the array with the given key value.
Array_fill_keys () Populates the array with the key value of the given key name.
Array_filter () Filters the elements in the array with a callback function.
Array_flip () Reverses/swaps the key names in the array and corresponding associated key values.
Array_intersect () Compares an array and returns the intersection of two arrays (only the key values are compared).
ARRAY_INTERSECT_ASSOC () Compares an array, returning the intersection of two arrays (comparing key names and key values).
Array_intersect_key () Compares an array and returns the intersection of two arrays (only the key names are compared).
ARRAY_INTERSECT_UASSOC () Compares an array, returns the intersection of two arrays (compares key names and key values, and uses a user-defined key-name comparison function).
Array_intersect_ukey () Compares arrays, returns the intersection of two arrays (only the key names are compared, using the user-defined key-name comparison function).
Array_key_exists () Checks whether the specified key name exists in the array.
Array_keys () Returns all the key names in the array.
Array_map () The user-defined function is scoped to each value of the given array, and the new value is returned.
Array_merge () Merges one or more arrays into an array.
Array_merge_recursive () Recursively merges one or more arrays into an array.
Array_multisort () Sorts multiple arrays or multidimensional arrays.
Array_pad () Inserts the specified number of elements with the specified value into the array.
Array_pop () Deletes the last element in the array (out of the stack).
Array_product () Computes the product of all the values in the array.
Array_push () Inserts one or more elements into the end of the array (into the stack).
Array_rand () Randomly selects one or more elements from the array, returning the key name.
Array_reduce () Iteratively simplifies the array to a string and returns by using a user-defined function.
Array_replace () Replaces the value of the first array with the value of the subsequent array.
Array_replace_recursive () Recursively replaces the value of the first array with the value of the subsequent array.
Array_reverse () Flips the order of elements in the original array, creates a new array, and returns.
Array_search () Searches the array for the given value and returns the corresponding key name if successful.
Array_shift () Deletes the first element in the array and returns the value of the deleted element.
Array_slice () Returns the selected part of the array.
Array_splice () Remove the specified elements from the array and replace them with other values.
Array_sum () Returns the and of all values in the array.
Array_udiff () Compares arrays, returns the difference set of two arrays (only the key values are compared, using a user-defined key-name comparison function).
ARRAY_UDIFF_ASSOC () Compares the array, returns the difference set of two arrays (compares key names and key values, uses the built-in function to compare key names, and uses user-defined functions to compare key values).
ARRAY_UDIFF_UASSOC () Compares arrays, returns the difference set of two arrays (compares key names and key values, and uses two user-defined key-name comparison functions).
Array_uintersect () Compares an array, returns the intersection of two arrays (only the key values are compared, using a user-defined key-name comparison function).
ARRAY_UINTERSECT_ASSOC () Compares an array, returns the intersection of two arrays (compares key names and key values, compares key names using the built-in function, and compares key values using a user-defined function).
ARRAY_UINTERSECT_UASSOC () Compares an array, returns the intersection of two arrays (compares key names and key values, and uses two user-defined key-name comparison functions).
Array_unique () Removes duplicate values from the array.
Array_unshift () Inserts one or more elements at the beginning of the array.
Array_values () Returns all the values in the array.
Array_walk () Applies a user function to each member in an array.
Array_walk_recursive () Each member of the array is recursively applied to the user function.
Arsort () Sorts the associative array in descending order of the key values.
Asort () Sorts the associative array in ascending order of key values.
Compact () Create an array that contains the variable names and their values.
Count () Returns the number of elements in the array.
Current () Returns the current element in the array.
each () Returns the current key/value pair in the array.
End () Points the inner pointer of the array to the last element.
Extract () Imports a variable from an array into the current symbol table.
In_array () Checks whether the specified value exists in the array.
Key () Gets the key name from the associative array.
Krsort () Sorts associative arrays in descending order of key names.
Ksort () Sorts associative arrays in ascending order of key names.
List () Assigns the values in the array to some array variables.
Natcasesort () Use the "natural sort" algorithm to sort the case-insensitive letters of an array.
Natsort () Use the "natural sort" algorithm to sort the array.
Next () Moves the inner pointer in the array backward one bit.
POS () The alias of current ().
Prev () Returns the internal pointer of the array back to one bit.
Range () Creates an array of elements that contain the specified range.
Reset () Points the inner pointer of the array to the first element.
Rsort () Sorts the numeric array in descending order.
Shuffle () Rearrange the elements in the array in random order.
sizeof () The alias of Count ().
Sort () Sorts the array of values in ascending order.
Uasort () Use a user-defined comparison function to sort the key values in the array.
Uksort () Use a user-defined comparison function to sort the key names in the array.
Usort () Use a user-defined comparison function to sort the array.
Related Article

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.