PHP Named Array Function

PHP Named Array Function thumbnail
PHP array functions allow web developers more flexibility in their code.

Programming languages such as PHP can store multiple pieces of data in a data structure called an "array," rather than storing a single piece of data in a single variable. PHP programmers can create these arrays with specific data, then reference it throughout the program. However, programmers can also use a number of specifically named array functions to perform a variety of operations with these arrays to extend their functionality and usefulness.

  1. PHP Arrays

    • PHP arrays work by storing a value, whether text or a number, according to an array key. This key can be either a text string or an integer. Calling the array with a given key as an argument will cause the key to return the value which is associated with that key. For example, if the text "first array element" is associated with the key "1," then calling the array and passing "1" as an argument will cause the array to return "first array element" as a text string.

    Using PHP Arrays

    • You can either declare a PHP array with any number of key and value pairings to start off with. Using the syntax "$PHPArray = array(1 => "First value", 2 => 5);" will create an array named "$PHPArray" with the text "First value" paired with the key "1" and key "2" paired with the integer value of five. You can use a PHP array's basic functionality of calling up values associated with particular keys with the syntax "$PHPArray[key]" where "key" is the key for the value you are trying to pull up. If you want to do more than simply call existing values from your Array, you can use a number of PHPs named array functions to perform advanced procedures on the array.

    Basic Named Array Functions

    • While simple PHP scripts can make due with arrays whose contents do not need to change during the course of scripts execution, more advanced scripts will require you to add and remove key and value pairings. You can add such pairs to the array using the function named "array_push." You can use this function with the syntax "array_push($PHPArray, key => value);" where "$PHPArray" is the name of your array, "key" is the key associated with the value, and "value" is the value itself. Similarly, you can remove individual array items with the "unset()" function. Use the syntax "unset($PHPArray[key])" to remove the key and its associated value from your array.

    Advanced Named Array Functions

    • Array functions also allow you to preform even more advanced operations on your arrays. For example, you can use the "array_rand()" function to pull a value out of your array by randomly selecting from on of the keys stored in the array. The "array_unique" function will go through your array and remove any key-value pairs whose value is the same as another value in your array. This will leave you with an array that consists entirely of unique values. You can find even more advanced array functions by consulting that PHP manual section.

Related Searches:

References

  • PHP Manual: Arrays
  • "PHP and MySQL Web Development"; Luke Welling and Laura Thomson; 2008

Resources

  • Photo Credit Comstock/Comstock/Getty Images

Comments

Related Ads

Featured