PHP basic array (1) _ PHP Tutorial

Source: Internet
Author: User
PHP base array (1 ). Arrays in PHP are actually an ordered graph. A graph is a type that maps values to keys. This type has been optimized in many ways, so you can regard it as a real array so that the array in PHP is actually an ordered graph. A graph is a type that maps values to keys. This type has been optimized in many ways, so you can use it as a real array, or a list (vector), a hash (an implementation of a graph), a dictionary, set, stack, queue, and more possibilities. Because another PHP array can be used as the value, and the tree can be easily simulated.

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

Syntax

Define array ()

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

Array ([key =>] value,...) // key can be integer or string // value can be any value

      "bar", 12 => true);echo $arr["foo"]; // barecho $arr[12];    // 1?> 

The key can be an integer or string. If the key name is an integer standard expression, it is interpreted as an integer (for example, "8" will be interpreted as 8, and "08" will be interpreted as "08 "). The variable type under the array in PHP does not affect the array. there is only one type of array, which can contain both integer and string subscript.

The value can be any value.

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

If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. If the specified key name already has a value, the value will be overwritten.

      43, 32, 56, "b" => 12);// ...this arrayarray(5 => 43, 6 => 32, 7 => 56, "b" => 12);?>  

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

Arrays and objects cannot be used as key names. This will cause a warning: Illegal offset type.

You can use square brackets to create or modify an existing array by explicitly setting the value.

This is achieved by specifying a key name in square brackets to assign values to the array. You can also omit the key name. in this case, add an empty square brackets ("[]") to the variable name.

$ Arr [key] = value; $ arr [] = value; // key can be integer or string // value can be any value.

If $ arr does not exist, a new one is created. This is also a replacement method for defining arrays. To change a value, just assign it a new value. If you want to delete a key/value pair, you need to use unset () for it ().

      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?> 

Note:As mentioned above, if you provide square brackets but do not specify a key name, take the current maximum integer index value. The new key name is the value + 1. If no integer index exists, the key name is0. If the specified key name already has a value, the value will be overwritten.


Arrays in the http://www.bkjia.com/PHPjc/446742.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446742.htmlTechArticlePHP are actually an ordered graph. A graph is a type that maps values to keys. This type has been optimized in many ways, so you can regard it as a real array to make...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.