The index of the PHP array learning notes

Source: Internet
Author: User
Tags foreach numeric

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/> ";
}

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.