PHP ArraysAn array stores multiple values in a single variable. You have already learnt that a variable is a storage area holding numbers and text. The problem is, a variable will hold only one value. An array is a special variable, which can hold more than one value, at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1="Saab"; $cars2="Volvo"; $cars3="BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The best solution here is to use an array! An array can hold all your variable values under a single name. And you can access the values by referring to the array name. Each element in the array has its own index so that it can be easily accessed. - In PHP, there are three kind of arrays:
- Numeric array - An array with a numeric index
- Associative array - An array where each ID key is associated with a value
- Multidimensional array - An array containing one or more arrays
A numeric array stores each array element with a numeric index. There are two methods to create a numeric array. 1. In the following example the index are automatically assigned (the index starts at 0): $cars=array("Saab","Volvo","BMW","Toyota");
2. In the following example we assign the index manually: $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; Example: In the following example you access the variable values by referring to the array name and index: Code: <?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?> An output of the code above will be: Saab and Volvo are Swedish cars.
An associative array, each ID key is associated with a value. When storing data about specific named values, a numerical array is not always the best way to do it. With associative arrays we can use the values as keys and assign values to them. Example 1: In this example we use an array to assign ages to the different persons: $ages = array("Jimmy"=>26, "Lara"=>25, "Vince"=>34); Example 2: This example is the same as example 1, but shows a different way of creating the array: $ages['Jimmy'] = "26"; $ages['Lara'] = "25"; $ages['Vince'] = "34";
The ID keys can be used in a script: Code: <?php $ages['Jimmy'] = "26"; $ages['Lara'] = "25"; $ages['Vince'] = "34";
echo "Jimmy is " . $ages['Jimmy'] . " years old.";
?> An output of the code above will be: Jimmy is 26 years old.
In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Example 1: In this example we create a multidimensional array, with automatically assigned ID keys: Code: <?php $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); ?> The array above would look like this if written to the output: Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) ) Example 2 Lets try displaying a single value from the array above: echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?"; The code above will output: Is Megan a part of the Griffin family?
The array functions allow you to manipulate arrays. PHP supports both simple and multi-dimensional arrays. There are also specific functions for populating arrays from database queries. PHP: indicates the earliest version of PHP that supports the function. | Function | Description | PHP | | array() | Creates an array | 3 | | array_change_key_case() | Returns an array with all keys in lowercase or uppercase | 4 | | array_chunk() | Splits an array into chunks of arrays | 4 | | array_combine() | Creates an array by using one array for keys and another for its values | 5 | | array_count_values() | Returns an array with the number of occurrences for each value | 4 | | array_diff() | Compares array values, and returns the differences | 4 | | array_diff_assoc() | Compares array keys and values, and returns the differences | 4 | | array_diff_key() | Compares array keys, and returns the differences | 5 | | array_diff_uassoc() | Compares array keys and values, with an additional user-made function check, and returns the differences | 5 | | array_diff_ukey() | Compares array keys, with an additional user-made function check, and returns the differences | 5 | | array_fill() | Fills an array with values | 4 | | array_filter() | Filters elements of an array using a user-made function | 4 | | array_flip() | Exchanges all keys with their associated values in an array | 4 | | array_intersect() | Compares array values, and returns the matches | 4 | | array_intersect_assoc() | Compares array keys and values, and returns the matches | 4 | | array_intersect_key() | Compares array keys, and returns the matches | 5 | | array_intersect_uassoc() | Compares array keys and values, with an additional user-made function check, and returns the matches | 5 | | array_intersect_ukey() | Compares array keys, with an additional user-made function check, and returns the matches | 5 | | array_key_exists() | Checks if the specified key exists in the array | 4 | | array_keys() | Returns all the keys of an array | 4 | | array_map() | Sends each value of an array to a user-made function, which returns new values | 4 | | array_merge() | Merges one or more arrays into one array | 4 | | array_merge_recursive() | Merges one or more arrays into one array | 4 | | array_multisort() | Sorts multiple or multi-dimensional arrays | 4 | | array_pad() | Inserts a specified number of items, with a specified value, to an array | 4 | | array_pop() | Deletes the last element of an array | 4 | | array_product() | Calculates the product of the values in an array | 5 | | array_push() | Inserts one or more elements to the end of an array | 4 | | array_rand() | Returns one or more random keys from an array | 4 | | array_reduce() | Returns an array as a string, using a user-defined function | 4 | | array_reverse() | Returns an array in the reverse order | 4 | | array_search() | Searches an array for a given value and returns the key | 4 | | array_shift() | Removes the first element from an array, and returns the value of the removed element | 4 | | array_slice() | Returns selected parts of an array | 4 | | array_splice() | Removes and replaces specified elements of an array | 4 | | array_sum() | Returns the sum of the values in an array | 4 | | array_udiff() | Compares array values in a user-made function and returns an array | 5 | | array_udiff_assoc() | Compares array keys, and compares array values in a user-made function, and returns an array | 5 | | array_udiff_uassoc() | Compares array keys and array values in user-made functions, and returns an array | 5 | | array_uintersect() | Compares array values in a user-made function and returns an array | 5 | | array_uintersect_assoc() | Compares array keys, and compares array values in a user-made function, and returns an array | 5 | | array_uintersect_uassoc() | Compares array keys and array values in user-made functions, and returns an array | 5 | | array_unique() | Removes duplicate values from an array | 4 | | array_unshift() | Adds one or more elements to the beginning of an array | 4 | | array_values() | Returns all the values of an array | 4 | | array_walk() | Applies a user function to every member of an array | 3 | | array_walk_recursive() | Applies a user function recursively to every member of an array | 5 | | arsort() | Sorts an array in reverse order and maintain index association | 3 | | asort() | Sorts an array and maintain index association | 3 | | compact() | Create array containing variables and their values | 4 | | count() | Counts elements in an array, or properties in an object | 3 | | current() | Returns the current element in an array | 3 | | each() | Returns the current key and value pair from an array | 3 | | end() | Sets the internal pointer of an array to its last element | 3 | | extract() | Imports variables into the current symbol table from an array | 3 | | in_array() | Checks if a specified value exists in an array | 4 | | key() | Fetches a key from an array | 3 | | krsort() | Sorts an array by key in reverse order | 3 | | ksort() | Sorts an array by key | 3 | | list() | Assigns variables as if they were an array | 3 | | natcasesort() | Sorts an array using a case insensitive "natural order" algorithm | 4 | | natsort() | Sorts an array using a "natural order" algorithm | 4 | | next() | Advance the internal array pointer of an array | 3 | | pos() | Alias of current() | 3 | | prev() | Rewinds the internal array pointer | 3 | | range() | Creates an array containing a range of elements | 3 | | reset() | Sets the internal pointer of an array to its first element | 3 | | rsort() | Sorts an array in reverse order | 3 | | shuffle() | Shuffles an array | 3 | | sizeof() | Alias of count() | 3 | | sort() | Sorts an array | 3 | | uasort() | Sorts an array with a user-defined function and maintain index association | 3 | | uksort() | Sorts an array by keys using a user-defined function | 3 | | usort() | Sorts an array by values using a user-defined function | 3 |
PHP: indicates the earliest version of PHP that supports the constant. | Constant | Description | PHP | | CASE_LOWER | Used with array_change_key_case() to convert array keys to lower case | | | CASE_UPPER | Used with array_change_key_case() to convert array keys to upper case | | | SORT_ASC | Used with array_multisort() to sort in ascending order | | | SORT_DESC | Used with array_multisort() to sort in descending order | | | SORT_REGULAR | Used to compare items normally | | | SORT_NUMERIC | Used to compare items numerically | | | SORT_STRING | Used to compare items as strings | | | SORT_LOCALE_STRING | Used to compare items as strings, based on the current locale | 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 | | | PHP WHILE LOOP
|