How an array is declared in php?
An array stores multiple values in one single variable: Show
Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » What is an Array?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 = "Volvo"; 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 solution is to create an array! An array can hold many values under a single name, and you can access the values by referring to an index number. Create an Array in PHPIn PHP, the In PHP, there are three types of arrays:
Get The Length of an Array - The count() FunctionThe Example $cars = array("Volvo", "BMW", "Toyota"); Try it Yourself » Complete PHP Array ReferenceFor a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! PHP ExercisesAn array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible. Explanation of those data structures is beyond the scope of this manual, but at least one example is provided for each of them. For more information, look towards the considerable literature that exists about this broad topic. SyntaxSpecifying with array() An array
can be created using the array() language construct. It takes any number of comma-separated array( key => value, key2 => value2, key3 => value3, ... ) The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e.
Example #1 A simple array
The key can either be an int or a string. The value can be of any type. Additionally the following key casts will occur:
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten. Example #2 Type Casting and Overwriting example
The above example will output: array(1) { [1]=> string(1) "d" } As all the keys in the above example are cast to PHP arrays can contain int and string keys at the same time as PHP does not distinguish between indexed and associative arrays. Example #3 Mixed int and string keys
The above example will output: array(4) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" [100]=> int(-100) [-100]=> int(100) } The key is optional. If it is not specified, PHP will use the increment of the largest previously used int key. Example #4 Indexed arrays without key
The above example will output: array(4) { [0]=> string(3) "foo" [1]=> string(3) "bar" [2]=> string(5) "hello" [3]=> string(5) "world" } It is possible to specify the key only for some elements and leave it out for others: Example #5 Keys not on all elements
The above example will output: array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [6]=> string(1) "c" [7]=> string(1) "d" } As you can see the last value Example #6 Complex Type Casting and Overwriting example This example includes all variations of type casting of keys and overwriting of elements.
The above example will output: array(7) { [1]=> string(1) "g" [-1]=> string(1) "d" ["01"]=> string(1) "e" ["1.5"]=> string(1) "f" [0]=> string(1) "h" [""]=> string(1) "j" [2]=> string(1) "l" } Accessing array elements with square bracket syntax Array elements can be
accessed using the Example #7 Accessing array elements
The above example will output: string(3) "bar" int(24) string(3) "foo"
Example #8 Array dereferencing
Creating/modifying with square bracket syntaxAn existing array can be modified by explicitly setting values in it. This is done by assigning values to the array, specifying the key in brackets. The key can also be omitted, resulting in an empty pair of brackets ( $arr[key] = value; $arr[] = value; // key may be an int or string // value may be any value of any type If $arr doesn't exist yet or is set to
To change a certain value, assign a new value to that element using its key. To remove a key/value pair, call the unset() function on it.
Array destructuring Arrays can be destructured using the
$foo, $bar, $baz] = $source_array; Array destructuring can be used in foreach to destructure a multi-dimensional array while iterating over it.
$source_array as [$id, $name]) { Array elements will be ignored if
the variable is not provided. Array destructuring always starts at index
$baz; // prints "baz" As of PHP 7.1.0, associative arrays can be destructured too. This also allows for easier selection of the right element in numerically indexed arrays as the index can be explicitly specified.
$three; // prints 3$source_array = ['foo', 'bar', 'baz'];// Assign the element at index 2 to the variable $baz Array destructuring can be used for easy swapping of two variables.
$b, $a] = [$a, $b];
Useful functionsThere are quite a few useful functions for working with arrays. See the array functions section.
The foreach control structure exists specifically for arrays. It provides an easy way to traverse an array. Array do's and don'tsWhy is |