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