The values stored in the array are called array elements, and each array element has an associated anchor (also known as a keyword) that can be used to access the element. PHP allows for spacer use of numbers or strings as an array of pins. Using strings as an anchor is more meaningful and easier to use.
Array of numeric indices
To create an array, you can use the following code:
$num = Array (1, 2, 3, 4, 5, 6);
The above code creates an array named $num that contains the number from 1~6,array () is a language structure, not a function.
If you need to store numbers in ascending order in an array, you can use the range () function to automatically create the array.
$num 1 = range (1, 10);
This line of code creates a 1~10 array of numbers. The range () function can also manipulate characters.
To access an array, you can use a combination of variable names and keywords or indexes to visit its contents. Using $num [0], you can use the data in the array $num. In PHP, the default value for a digital index starts at 0.
$num [0] = 1;
A numeric array uses an ordered number as the anchor, and you can use a for loop to display the contents of the array:
for ($i = 0; $i <6; i++) {
echo $num [$i];
}
Non-numeric indexed array
In PHP, arrays do not need to be initialized or created.
$products [0] = rice;
$products [1] = milk;
This creates an array $products, and the first line of code is to create an array of rice elements.
You can also use => to associate each variable value with a keyword.
$prices = Array (' Rice ' =>, ' milk ' => 20);
As before, you can use the $prices [' Rice '] to access the array, which is also a common way of daily use.
Because the index of a non-numeric indexed array is a keyword, you can use the Foreach Loop or the list () and each () structure for circular access.
foreach Loop
foreach ($prices as $key => $value) {
echo $key. " -". $value." <br/> ";
}
each () structure
while ($element = each ($prices)) {
echo $element [' key '];
echo "-";
echo $element [' value '];
echo "<br/>";
}
each () This function returns the current element of the array and takes the next element as the current element. Because the each () function is called in the while loop, it returns each element in the array sequentially, and when it reaches the end of the array, the loop operation terminates.
List () structure
while (the list ($produce, $price) = each ($pirces) {
echo "$product-$" proce<br/> ";
}