PHP------Define arrays, array of values, and iterate arrays

Source: Internet
Author: User

PHP arrays

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

What is an index?

is a common array of styles, index from the beginning, 0,1,2,3, the definition of the array is directly inside the value, only one index automatically generated, so generally starting from 0, such an array is an indexed array, the index is continuous.

What is an association?

Is our hash table collection, when defined, must give it a key, a values, the two are related, through the key corresponding values are associated.

1. Defining arrays

The first way to define an array:

To define a simple indexed array

$a = Array (+/-);

The second way to define an array:

Assignment definition

$a [] = 1;

$a [] = 2;

$a [] = 3;

The third way to define an array:

Defining associative arrays

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9

);

2. Array values

To take a value from an indexed array:

$a = Array (+/-);

echo $a [0];

The value value is taken by key:

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9

);

echo $a ["three"];

3. Iterating through an array

(1). For loop

Applies only to indexed arrays

$a = Array (+/-);

for ($i =0; $i <count ($a); $i + +)

{

echo $a [$i]. " <br> ";

}

(2). foreach Traversal

Applies to all arrays

The first form of foreach:

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9);

foreach ($a as $b)

{

echo $b. " <br> ";

}

The second form of foreach:

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9);

foreach ($a as $b = $c)

{

echo $b. " <> ". $c." <br> ";

You can also write this:

echo "{$b}<>{$c}<br>";

}

(3). Apply each () and list () to iterate through the array

Returns the details of the current element inside the array

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9);

Var_dump (each ($a));

Var_dump (each ($a));

Var_dump (each ($a));

(4). Assigns each element in the right array to the list () argument lists

Note: The right array must contain an index

$a = Array (+/-);

List ($b, $c, $d) = $a;

Echo $b;

Echo $c;

Echo $d;

While traversal

(5). For each () and list () combined array traversal

$a = Array (

"One" = "Hello",

"=>100",

"Three" =>9.9);

while (list ($b, $c) = each ($a))

{

echo "{$b}--{$c}<br>";

}

PHP------Define arrays, array of values, and iterate 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.