Data type of array in PHP _php tutorial

Source: Internet
Author: User
In PHP, the array can hold the value can be any PHP type, if the given value does not specify the key name, then the current largest integer index value, and the new key name is the value plus one, this is the PHP array

There are three types of arrays:

Array of values
An array with a numeric ID key
Associative arrays
Each ID key in the array associates a value
Multidimensional arrays
An array that contains one or more arrays

The array in PHP is actually an ordered map. A map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. The value of an array element can also be another array. Tree structures and multidimensional arrays are also allowed.

You can use the array () language structure to create a new array. It accepts any number of key (key) = = VALUES (value) pairs separated by commas.

Key can be an integer or string. If key is a standard representation of an integer, it is interpreted as an integer (for example, "8" will be interpreted as 8, and "08" will be interpreted as "08"). The floating-point number in key is rounded to integer. Indexed arrays are the same as associative arrays in PHP, and can contain both integer and string subscripts.

The value can be any PHP type.

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

Use TRUE as the key name to make the integer 10% a key. Use FALSE as the key name to make the integer 0 a key. Using NULL as the key name is equivalent to using an empty string. Create a new (or overwrite) value with an empty string as the key name using an empty string, which is not the same as the empty square brackets.

You cannot use arrays and objects as keys (key). Doing so will result in a warning: illegal offset type.

New/modified with square brackets syntax
If the $arr does not already exist, a new one will be created. This is also a substitution method for defining arrays. To change a value, simply assign it a new value. If you want to delete a key name/value pair, use unset () for it.

Note: If the square brackets are given but no key name is specified, the current maximum integer index value is taken, and the new key name is the value + 1. If no integer index is currently present, the key name is 0. If the specified key name already has a value, the value will be overwritten.

Note that the maximum integer key name used here is not necessarily present in the array. It just has to exist after the last array rebuild index.

You should always enclose the array index with a string representation. For example, use $foo [' Bar '] instead of $foo [bar]. But why $foo [bar] wrong?

The reason is that there is an undefined constant (bar) in this code instead of a string (' bar '-note quotes), and PHP may define this constant later, but unfortunately your code has the same name. It works because PHP automatically converts a bare string (a string without a quotation mark) to a normal string whose value is the bare string, which does not correspond to any known symbol. For example, if no constant is defined as bar,php it will be substituted for ' bar ' and used.

Example 1
In this example, the ID key is automatically assigned:

The code is as follows Copy Code

$names = Array ("Peter", "Quagmire", "Joe");

Example 2
In this example, we manually assign the ID key:

The code is as follows Copy Code
$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe";

You can use these ID keys in your script:

The code is as follows Copy Code

$names [0] = "Peter";
$names [1] = "quagmire";
$names [2] = "Joe";

echo $names [1]. "and". $names [2]. "Is". $names [0]. "' s Neighbors";
?>

The output of the above code:

Quagmire and Joe are Peter ' s neighbors


Multidimensional arrays
In a multidimensional array, each element in the primary array is also an array. Each element in a sub-array can also be an array, and so on.

Example 1
In this example, we created a multidimensional array with an auto-assigned ID key:

The code is as follows Copy Code
$families = array
(
"Griffin" =>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire" =>array
(
"Glenn"
),
"Brown" =>array
(
"Cleveland",
"Loretta",
"Junior"
)
);


Summarize
Arrays in PHP are often used in our development, such as the same as the operation of multiple data volume available array key to save different values and other operations, arrays are the most rapid solution.

http://www.bkjia.com/PHPjc/629170.html www.bkjia.com true http://www.bkjia.com/PHPjc/629170.html techarticle in PHP, the array can hold the value can be any PHP type, if the given value does not specify the key name, then the current largest integer index value, and the new key will be the value of one, which is ...

  • 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.