Large class of arrays

Source: Internet
Author: User

Main content: Numeric index array, non-numeric index array, array operator, two-dimensional array and multidimensional array;

Numeric index Array

Create a new array

$phones Array (' HTC ', ' Moto ', ' Nokia ');

Or

$phones [0] = ' HTC '$phones[1] = ' moto '$phones[2] = ' Nokia;

For the second way to create an array, if $phones does not exist, $phones [0] = ' HTC ' creates an array of only one element, and subsequent operations will add new values to the array. The size of the array will change dynamically

Access the first, and second elements:

Echo $phones [0]. ' --‘. $phones [1];

If you need to keep the numbers in ascending order in the array, you can use the range () function, as follows: Create an array from 1 to 10

$number Range (1,10);

This function also has an optional third parameter, which allows the setting of the stride between values, such as creating an odd number from 1 to 10,

$number Range (1,10,2);

The range () function can also manipulate characters, such as a list of characters from A to Z, and you can do the following

$number Range (' A ', ' Z ');

To access an array:
1.for Cycle

$phones Array (' HTC ', ' Moto ', ' Nokia ');      for ($i= 0; $i<3; $i+ +)        {echo "$phones[$i]<br/>";}

2.foreach (specially designed for arrays)

$phones Array (' HTC ', ' Moto ', ' Nokia ');      foreach ($phones as$v) {        echo "$v<br/>";}
Non-numeric indexed arrays (related arrays)

In the $phones array, PHP is allowed to specify a default index. This means that the first element added is indexed as 0, the second is 1, and so on. PHP also supports associative arrays (non-numeric indexed arrays), collectively referred to as related arrays, that can associate the values of each variable with any keyword or index.

$phones Array (' htcG6 ' = 2400, ' motoI9 ' + 2500, ' nokiaN8 ' = 2890);

The key in the single quotation mark and the value behind it.
Accessing an array: Because the index in the related array is not a number, you cannot manipulate it with a for statement, but you can use the Foreach Loop or the list () and each () structure. Another foreach traversal loop array
1.foreach Access

foreach ($phonesas$key$value) {        echo "$key:$value <br/> ";}

2.each Access

 while ($elementeach ($phones)) {     echo$element//      echo ': ';      Echo $element //     echo ' <br/> ';}

The each () function returns the current element of the array and the next element as the current element, which is output sequentially in the while loop. $element is also an array. When the strip is used with each (), the position key or 0 contains the keyword of the current element, and the position value or 1 contains the value of the current element.

3.list Access

 while (list($name,$priceeach ($phones)) {     echo$ Name;      Echo ': ';      Echo $price ;      Echo ' <br/> ';}

The list () function and the $element variable in Method 2 are used in a similar way, but List is able to reassign custom variables and point to the array with the keywords and values of a set of elements. This is more convenient and clear than Method 2.

Array operators

Operations between arrays

Two-dimensional arrays and multidimensional arrays

A two-dimensional array, as well as a multidimensional array and a one-dimensional array, is similar to the loop, just on a one-dimensional basis, in the Addend group.

$phones Array (array(' Xiaomi ', ' Xiaomi Phone ', 1900),array(' HTC ', ' G6 ', 2400),array(' Nokia ', ' N8 ', 3200));

This array contains three arrays, each set, with products, product descriptions, and product price components. To access this array, you can use the following method.
Method One, use the numeric index directly

Echo ' | '. $phones [0] [0]. ' | '. $phones [0] [1]. ' | '. $phones [0] [2]. ' <br/> '; Echo ' | '. $phones [1] [0]. ' | '. $phones [1] [1]. ' | '. $phones [1] [2]. ' <br/> '; Echo ' | '. $phones [2] [0]. ' | '. $phones [2] [1]. ' | '. $phones [2] [2]. ' <br/> ';

Method Two, use the dual for loop

 for ($row= 0; $row<3; $row+ +)        {for ($col=0;  $col<3; $col+ +)            {echo ' | '. $phones [$row] [$col];        }         Echo ' <br/> ';}

Same as the result of the method one output.
Sometimes we may use the relevant array to save the data, which looks more intuitive, such as:

    $phones Array (array(' name ' = ' Xiaomi ', ' desc ' = ' millet phone ', ' price ' =>1900),              Array (' name ' = ' HTC ', ' desc ' = ' g6 ', ' Price ' =>2400),              array(' name ' = ' Nokia ', ' desc ' = ' n8 ', ' Price ' =>3200));

To traverse the data already on, you can use the following methods.
Method One

     for ($row= 0; $row<3; $row+ +)        {echo ' | '. $phones [$row] [' name ']. ' | '. $phones [$row] [' Desc ']. ' | '. $phones [$row] [' Price ']. ' <br/> ';    }

Method Two
Use for to traverse the outermost data, and then use the method of traversing one-dimensional related arrays.

     for ($row= 0; $row<3; $row+ +)        {while(thelist($key,$valueeach ( $phones [$row])) {            echo ' | $value";        }         Echo ' <br/> ';    }

Method One and method two result the same.
A three-dimensional array or a three-dimensional array is already on, similar to a two-dimensional array, that is, on a two-dimensional array in nested arrays. Traversal is also on the basis of two-dimensional traversal, in a nested layer.

Large class of 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.