PHP----Arrays

Source: Internet
Author: User

The definition of the PHP array.

Features: can store any type of data, can be discontinuous, can be indexed, or it can be associated

Index: is the style of the common array, index starting from 0, 0, 1, 2, 3, the definition of the array directly into the value, the index is automatically generated, so it is generally starting from 0, such an array is an indexed array. The index is contiguous.

Association:   is our hash table collection, at the time of definition, must give it a key, a value it two is related, I pass key its corresponding values, is associated.

Key,values can be understood as:??

In storage, not only a value, but a pair, this pair, a name called it, a code called its equivalent, I can take its name according to the code, code is key, the name is the values.

Benefits: is clearer.

No collection

First, define the array:

The first way to define "1":

Define simple, indexed data

$attr =aarray ()//define Array

() The value of the array can be written in parentheses

$attr =array (a); This is, the definition of simple, indexed data

The defined array only writes values of values:

array//keywords

The second way to define "2":

Assignment to define, no write index

$attr []=1;

$attr []=2;

Var_dump ($ATTR);

Example:

The results displayed:

"3" The Third Way:

Variable is attr

$attr

To define an associative array:

$attr =array ("key" = "values");

=< is the meaning of the corresponding values

Example:

$attr =array (

"One" = "Hello",//"key" and "values" a pair of writes

"=>100",//defines an array of associations

"Three" =>10.9

);

Var_dump ($ATTR);

Show Results:

Index Value

Associative arrays, you must have key and values.

If you define an associative array, do not write key, write only values, and automatically generate an indexed array.

Example:

The results displayed:

If you write a, the back of not write, what will happen??

Example:

The results displayed:

Automatically populated. Auto-populate with index

This is an associative array.

As long as there's a key inside.

Index, that is, starting from 0, must be continuous.

If any one of them is not the case above, it is called an association.

The results displayed:

In the definition of time and space, it does not fill the empty piece, it (index) only according to the above values, add.

In PHP, a string of 2 and a number of 2 is an effect, they can convert each other "2" = "22222",

Decimals are treated as strings, example 1:

The results displayed:

There are no decimals in the index.

Show Results:

indexes, which cannot be duplicated.

Second, the value of the PHP array.

"1" values are based on the index:

$attr [0];

Example:

The results displayed:

"2" takes value based on key

The associated array to take the value.

Example:

The results displayed:

Third, the PHP traversal array.

"1" is traversed with a for loop. Applies to indexed arrays, not to associative arrays

for ($i =0; $i < count (array name); $i + +) {}

The length of the array requires a method: Count (array name)

Example:

Show Results:

"1" foreach traversal, all applicable

<1>foreach (array value as parameter variable)

{

}

The value is given to the variable

Take value

Parameter variables are randomly named.

Example:

Show Results:

The results displayed:

<2> take key and value values

Example:

Show Results:

string to parse the variable

The results displayed:

"3"var_dump (each ($attr)); Returns the details of the current element inside the array

Each (a parameter, is an array)

Example 1:

Show Results:

"One" = "Hello"

Each () takes the first element and returns the array

The value of the reservation is comprehensive.

Example 2:

The results displayed:

List ($a, $b, $c) = $attr;//assigns each element in the right array a value of list ()

Note: The right array must contain an index 0,1,2,3

List () = array;

Example:

The results displayed:

The combination above, that is, to iterate over the array

List ($k, $v) =each ($attr);

{

echo "{$k}-{$v}<br>";

}

PHP----Arrays

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.