Chapter III using arrays (1)

Source: Internet
Author: User
Tags scalar

Learning Focus:

    • Numeric index Array
    • Non-numeric indexed arrays
    • Array operators
    • Multidimensional arrays
    • Array sorting
    • Array functions

In the first and second chapters, the variables I use are scalar variables (only a single value can be stored). Arrays can have a number of elements. PHP only eats numeric indexed arrays and associative arrays.

Associative arrays allow more meaningful data to be used as indexes. Each element can use a string or other more meaningful information as an index in addition to using a numeric index.

Array: A named range used to store a series of variable values. --Scalar Quantity: A named range that is used to store numeric values.

    • Numeric index Array

Initialize: Create a simple array, example: $products = Array (' Tires ', ' oil ', ' Spark plugs '), so that an array named $products is created with three fixed values-"Tires", "Oil", " Spark plugs "

where array () is actually a language structure, not a function.

If you need to save the required data to another array, you can copy the array to another array directly using "=": $products 2 = $products;

If you need to keep the numbers in ascending order in an array, you can use the range () function to create the array automatically: $number = Range (1,10), where the range () function has a third optional parameter, which

Allow the stride length between values, for example we can create an array of 1,3,5,7,9: $number = range (1,10,2); (where stride is 2)

The range () function can also manipulate characters: $letter = range (' A ', ' Z ');

Accessing the contents of an array: To access the contents of a variable, you can use its name directly;

If the variable is an array, use a combination of the variable name and keyword or index to access its contents.

Example: Using $products[0], $products [1], $products [2] will be able to access the contents of the $products array.

To iterate through an array:

// Create an array $products Array (' Tires ', ' oil ', ' Spark plugs ');  for ($i = 0; $i < 3; $i+ +)    {echo$products[$i]. " ";

The example results are: Tires oil Spark plugs

You can also use the Foreach Loop, which is:

// Create an array $products Array (' Tires ', ' oil ', ' Spark plugs '); foreach ($productsas$current) {    echo$current. ";}

The sample result remains: Tires oil Spark plugs

    foreach syntax structure: can be used only for arrays and objects. There are two ways to use it:

"1" foreach (Array_expression as $value)

Statement

"2" foreach (array_expression as $key = $value)

Statement

The first format iterates through the given array of array_expression, each time the value of the current cell is assigned to the $value and the pointer inside the array is moved one step forward (so the next cell in the next loop will be taken).

The second format does the same thing, except that the key name of the current cell is assigned to the variable $key in each loop .

TIPS: You can modify the elements of an array by adding & to it before $value.

<? PHP $arr Array (1, 2, 3, 4); foreach ($arr as &$value) {    $value$value * 2;} // $arr is now Array (2, 4, 6, 8) unset ($value//  finally cancel the reference ?>

Note that the $value reference is only available if the traversed array can be referenced, and the following code cannot be run:

<? PHP foreach (array as &$value) {    $value$value * 2;}? >
    • Using arrays of different indexes

In an associative array, you can associate each variable value with any keyword or index.

Initialize associative array: $prices = Array (' Tires ' = +, ' oil ' =, ' Spark plugs ' = 4); This allows you to create a product name as a keyword, with a price as a worthy associative array.

Access to array elements: You can access the contents of an array using variable names and keywords, so you can use $prices[' Tires ', $prices [' oil '], $prices [' Spark plugs '] to access the information in the prices array.

Use a looping statement: (because the index of an associative array is not a number, you cannot operate with a counter on a group), using a Foreach Loop or list () and each () structure

When using the Foreach loop, you can use this:

$prices Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4); foreach ($pricesas$current) {    echo$current. ";}

The sample results are: 100 10 4

You can also do this:

$prices Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4); foreach ($pricesas$key$value) {    echo$key. "-".  $value. " <br/> ";}

The example results are:

              Here is the contents of the $prices array printed with the each () structure:

$prices Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4);  while ($elementeach ($prices)) {    echo$element[' key '];     echo "-";     Echo $element [' Value '];     Echo "<br/>";}

The example results are:

The each () function returns the current element of the array and the next element as the current element. The each () function is called in the while loop, which returns each element in the array sequentially, and when it reaches the end of the array, the loop operation terminates.

             The following is the use of the list () function:

$prices Array (' Tires ' =>100, ' oil ' =>10, ' Spark plugs ' =>4);  while (list($product,$priceeach ($prices)) {    Echo "$product$price<br/>";}

The code above uses each () to remove the current element from the $prices array and return it as an array, and then point to the next element. It also uses list () to change the array returned from each () to contain 0, 12 elements into two new variables named $product and $price.

    • Array operators

Chapter III using arrays (1)

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.