Manual
H1 class=sect1>
An array in PHP is actually a sequential mapping. A map is a map value to the keyword. This type is optimized on a separate method, and you can use it as a real array or as a list (vector), Hashtable (a map of execution), dictionaries, aggregates, stacks, queues and more. Because you might have another php-array as a value, you can also imitate the tree structure very easily.
The interpretation of this structure is beyond the scope of this manual, but one will find the smallest paradigm for this structure. For more information on this structure, please consult other literature.
Array () specifies arrays
An array can be constructed by array (). It consists of a pair of key = value and a series of numbers separated by commas.
A key is any non-negative integer or a string composition. If one is expressed by a standard nonnegative integer, it will be interpreted as such (i.e. ' 8 ' will be interpreted as 8, ' 08 ' will be interpreted as ' 08 ').
A value can be arbitrary.
Ignore the key. If you omit a key, the new key will be added with the largest integer index. If the integer index does not exist, the key will be 0. If you have specified a value for a key, then this will be covered.
Array ([key =>] Value
, ...
)
The key is any string or non-negative integer
The value can be arbitrary
You can modify an existing array by setting the value explicitly.
You can assign a value to an array with a key with square brackets. You can also ignore this key and add a pair of empty brackets after the variable name. $arr [key] = value;
$arr [] = value;
Key is any string or non-negative integer
Value can be any of the
If $arr does not exist, it will be created. It is also possible to selectively specify an array. To change a certain value, just assign a new value to it. If you want to remove a pair of keys/values, you need to use unset ().
For the work of an array, there is a useful function, see the paragraph of the arrays function.
The Foreach Process control explicitly provides an easy way to loop an array.
You may have seen the following syntax in the old script:
$foo [Bar] = ' enemy ';
echo $foo [bar];
etc
This is wrong, but it will work. However, why is it wrong? After this, the syntax fragment specifies that the expression must be between square brackets. This means that you can do the following as below:
echo $arr [foo (true)];
This example uses the return value of a function as an index to the array. PHP also knows is constant, you can see e_*.
$error _descriptions[e_error] = "A fatal error has occured";
$error _descriptions[e_warning] = "PHP issued a WARNING";
$error _descriptions[e_notice] = "Just an informal NOTICE";
Note that E_error is a valid identifier, just like the bar in the first example. But the last example was in fact the same as writing:
$error _descriptions[1] = "A fatal error has occured";
$error _descriptions[2] = "PHP issued a warning";
$error _descriptions[8] = "Just an informal notice";
Because E_error equals 1, etc.
And how's it possible that $foo [bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes so meant bar literally, as the string "bar", but so you forgot to write the quotes.
At some a, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this, since they is special keywords.
And, if these arguments don ' t help:this syntax is simply deprecated, and it might stop working some day.
Tip:when you turn error_reporting to E_all and you'll see that PHP generates warnings whenever this construct is used. This was also valid for other deprecated ' features '. (Put the line error_reporting (E_all), in your script)
Note:inside a double-quoted string, an and syntax is valid. See variable parsing in strings for more details.
The array type in PHP was very versatile, so here'll be some examples to show you the full power of arrays.
This
$a = array (' Color ' = ' red '
, ' taste ' = ' sweet '
, ' shape ' = ' round '
, ' name ' = ' Apple '
, 4//key would be 0
);
is completely equivalent with
$a [' color '] = ' red ';
$a [' taste '] = ' sweet ';
$a [' shape '] = ' round ';
$a [' name '] = ' apple ';
$a [] = 4; Key'll be 0
$b [] = ' a ';
$b [] = ' B ';
$b [] = ' C ';
Would result in the array array (0 = ' a ', 1 = ' B ', 2 = ' C '),
or simply Array (' A ', ' B ', ' C ')
Example 6-4. Using Array ()
Array as (property-) map
$map = Array (' Version ' = 4
, ' OS ' = ' Linux '
, ' lang ' = ' 中文版 '
, ' short_tags ' = True
);
Strictly numerical keys
$array = Array (7
, 8
, 0
, 156
,-10
);
This is the same as array (0 = 7, 1 = 8, ...)
$switching = Array (Ten//key = 0
, 5 = 6
, 3 = 7
, ' a ' = 4
, one//key = 6 (maximum of integer-indices was 5)
, ' 8 ' = 2//key = 8 (integer!)
, ' in the ' +//key = ' 02 '
, 0 =//The value of would be overwritten by 12
);
Empty array
$empty = Array ();
Example 6-5. Collection
$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');
foreach ($colors as $color) {
echo "do $color? \ n";
}
/* Output:
Like red?
Do you like blue?
Do you like green?
Do you like yellow?
*/
Note that it was currently not possible to change the values of the array directly in such a loop. A workaround is the following:
Example 6-6. Collection
foreach ($colors as $key = = $color) {
Won ' t work:
$color = Strtoupper ($color);
Works
$colors [$key] = Strtoupper ($color);
}
Print_r ($colors);
/* Output:
Array
(
[0] = RED
[1] = BLUE
[2] = GREEN
[3] = YELLOW
)
*/
This example creates a one-based array.
Example 6-7. One-based Index
$firstquarter = Array (1 = ' January ', ' February ', ' March ');
Print_r ($firstquarter);
/* Output:
Array
(
[1] = ' January '
[2] = ' February '
[3] = ' March '
)
*/
Example 6-8. Filling Real Array
Fill an array with any items from a directory
$handle = Opendir ('. ');
while ($file = Readdir ($handle))
{
$files [] = $file;
}
Closedir ($handle);
Arrays is ordered. You can also the order using various sorting-functions. See Array-functions for more information.
Example 6-9. Sorting array
Sort ($files);
Print_r ($files);
Because the value of an array can is everything, it can also be another array. This is the can make recursive and multi-dimensional arrays.
Example 6-10. Recursive and multi-dimensional arrays
$fruits = Array ("fruits" = = Array ("A" = "orange"
, "b" = "banana"
, "c" = "apple"
)
, "numbers" = = Array (1
, 2
, 3
, 4
, 5
, 6
)
, "holes" = = Array ("First"
, 5 = "Second"
, "Third"
)
);