PHP Basics Array (1)_PHP Tutorial

WBOY
Release: 2016-07-15 13:24:30
Original
745 people have browsed it

An array in PHP is actually an ordered graph. A graph is a type that maps values ​​to keys. This type is optimized in many ways, so you can use it as a real array, or a list (vector), a hash table (an implementation of a graph), a dictionary, a set, a stack, a queue, and many more possibilities . Since you can use another PHP array as a value, you can also simulate a tree easily.

Explaining these structures is beyond the scope of this manual, but you will find at least one example for each structure. For more information on these structures, we recommend that you consult external works on this broad topic.

Syntax

Definition array()

You can use the array() language structure to create a new array. It accepts a certain number of comma-separated key => value parameter pairs.

array( [key =>]value     , ...     )// key 可以是 integer 或者 string// value 可以是任何值
Copy after login

<?php $arr = array("foo" => "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12];    // 1?> 
Copy after login

key can be integer or string. If the key name is an integer in the standard representation, it is interpreted as an integer (e.g. "8" will be interpreted as 8, and "08" will be interpreted as "08"). The variable type of array subscripts in PHP will not affect the array. There is only one type of array, which can contain both integer and string subscripts. The

value can be any value.

<?php $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));echo $arr["somearray"][6];    // 5echo $arr["somearray"][13];  // 9echo $arr["somearray"]["a"];  // 42?>  
Copy after login

If no key is specified for the given value, the current largest integer index value is taken, and the new key name will be that value plus one. If the key name you specify already has a value, the value will be overwritten.

<?php // This array is the same as ...array(5 => 43, 32, 56, "b" => 12);// ...this arrayarray(5 => 43, 6 => 32, 7 => 56, "b" => 12);?>  
Copy after login

Using TRUE as the key name will make integer 1 the key name. Using FALSE as the key name will make integer 0 the key name. Using NULL as a key name is equivalent to using an empty string. Using an empty string as a key will create (or overwrite) a value with an empty string as the key, which is different from using empty square brackets.

Arrays and objects cannot be used as key names. Doing so will result in a warning: Illegal offset type.

New/Modify using square bracket syntax can change an existing array by explicitly setting the value.

This is accomplished by assigning values ​​to the array by specifying key names within square brackets. You can also omit the key name, in which case add an empty pair of square brackets ("[]") to the variable name.

$arr[key] = value;$arr[] = value;// key 可以是 integer 或者 string// value 可以为任何值。
Copy after login

If $arr does not exist yet, a new one will be created. This is also an alternative way to define an array. To change a value, just assign it a new value. If you want to delete a key/value pair, use unset() on it.

<?php $arr = array(5 => 1, 12 => 2);$arr[] = 56;    // This is the same as $arr[13] = 56;               // at this point of the script$arr["x"] = 42; // This adds a new element to               // the array with key "x"unset($arr[5]); // This removes the element from the arrayunset($arr);    // This deletes the whole array?> 
Copy after login

Note: As mentioned above, if you give square brackets but do not specify a key name, the current maximum integer index value will be taken and the new key name will be Value + 1. If there is no integer index currently, the key name will be 0. If the specified key name already has a value, the value will be overwritten.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446742.htmlTechArticleThe array in PHP is actually an ordered graph. A graph is a type that maps values ​​to keys. This type is optimized in many ways, so you can use it as a real array...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!