Data Types of arrays in php

Source: Internet
Author: User

In php, the array can store values of any PHP type. If no key name is specified for the given value, the maximum integer index value is used, the new key name is the value plus one, which is the php array.

There are three types of Arrays:

Numeric Array
Array with digit ID key
Join Array
Each ID key in the array is associated with a value.
Multi-dimensional array
Array containing one or more Arrays

Arrays in PHP are actually an ordered ing. Valuing is a type that associates values with keys. This type has been optimized in many aspects, so it can be considered as a real array, or a list (vector), a hash list (an implementation of ing), a dictionary, a set, stack, queue, and more possibilities. The value of an array element can also be another array. Tree Structure and multi-dimensional array are also allowed.

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

The key can be an integer or string. If the key is a standard expression 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 the key is rounded to integer. In PHP, the index array and the associated array are the same. They can both contain the subscript of the integer and string.

The value can be of any PHP type.

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.

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 keys ). This will cause a warning: Illegal offset type.

Create/modify with square brackets
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 ().

Note: If square brackets are provided but no key name is specified, the current maximum integer index value is used. The new key name is the value + 1. If no integer index exists, 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 used here is not necessarily in the array currently. It only needs to exist after the index is re-generated in the last array.

Always put quotation marks on the array index represented by a string. For example, use $ foo ['bar'] instead of $ foo [bar]. But why is $ foo [bar] Wrong?

The reason is that this code has an undefined constant (bar) instead of a string ('bar'-note quotation marks), and PHP may define this constant later, unfortunately, your code has the same name. It can run because PHP automatically converts a bare string (a string without quotation marks and does not conform to any known symbol) into a normal string whose value is the bare string. For example, if no constant is defined as bar, PHP replaces it with 'bar' and uses it.

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 allocate 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 the script:

The Code is as follows: Copy code

<? Php

$ Names [0] = "Peter ";
$ Names [1] = "Quagmire ";
$ Names [2] = "Joe ";

Echo $ names [1]. "and". $ names [2]. "are". $ names [0]. "'s neighbors ";
?>

Output of the above Code:

Quagmire and Joe are Peter's neighbors


Multi-dimensional array
In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.

Example 1
In this example, we create a multidimensional array with an automatically assigned ID key:

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


Summary
Arrays in php are often used in our development. For example, if you want to operate multiple data volumes and use array keys to save different values, arrays are the most rapid solution.

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.