![]() |
|
PHP array functions. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | PHP Array Functions |
|
|
The following functions are part of the numerous functions that make up the PHP language. This is a very small list of what the PHP offers. These are among the most useful of PHP functions that a PHP developer needs to use regularly. If you are interested and want more please visit the PHP manual at http://www.php.net/manual. PHP Array Functions Various PHP functions are available for use with arrays. The most useful of them are discussed here. PHP array() function The array() function allows us to manually assign values to an array. Here is how this works: $array_name = array("value1", "value2", "value3", ...); PHP array_push() function The array_push() function allows us to add one or more elements to the end of an existing array. Let's see how this works: array_push($array_name, "element 1", "element 2", "element 3", ...); PHP array_pop() function The array_pop() function allows us to take (pop) off the last element of an existing array. It return the last element of the array and removes the same from the array. Let's see how this works: array_pop($array_name); PHP array_merge() function The array_merge() function allows us to combine two or more existing arrays. Here is how this works: array_merge($array1, $array2, ...); PHP array_keys() function The array_keys() function returns an array of all the key names in an existing array. Its syntax follows: array_keys($array_name); PHP array_values() function The PHP array_values() function returns an array of all the values in an existing array. The syntax follows: array_values($array_name); PHP count() function The PHP count() function returns the number of elements in a variable. We usually use this function to get the number of elements of an array, as a variable that is not an array has only one element. Let's see how this works: $numberOfColors = count($colors); PHP reset() function The PHP reset() function resets the pointer to the beginning of the array. Here is how it works: reset($array_name); PHP shuffle() function The PHP shuffle() function randomizes the elements of a given array. Its syntax follows: shuffle($array_name); PHP sizeof() function The PHP sizeof() function returns the number (count) of elements in an array. It works similar to the PHP count() function. Let's see it syntax: $numberOfColors = count($colors); |