Each array of PHP collation collection

Source: Internet
Author: User

An array can store one or more values in a separate variable name.

eg

<? PHP $cars=array("Volvo", "BMW", "SAAB"echo$cars$cars$cars [2]. "." ;? >

显示: I like Volvo, BMW and SAAB.

In PHP, the array () function is used to create arrays

Indexes are automatically assigned (index starting from 0):

$cars =array ("Volvo", "BMW", "SAAB");

Or you can manually assign an index:

$cars [0]= "Volvo";  $cars [1]= "BMW"; $cars [2]= "SAAB";
The count () function returns the length of the array (number of elements)
Iterating through an indexed array

To iterate through and output all the values of an indexed array, you can use a for loop, like this:

<? PHP $cars=array("Volvo", "BMW", "SAAB"); $arrlength=count($cars);  for ($x= 0; $x<$arrlength; $x+ +)   {echo$cars[$x]   ; Echo "<br>";}? >

Show as Volvo
BMW
SAAB

PHP Associative array

An associative array is an array that uses the specified key that you assign to the array.

There are two ways to create an associative array:

$age =array ("Peter" and "a", "Ben" = "Notoginseng", "Joe" and "43");

Or:

$age [' Peter ']= '; $age [' Ben ']= "Notoginseng"; $age [' Joe ']= "43";

You can then use the specified key in the script:

<? PHP $age=array("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "+"); Echo $age [' Peter ']. "Years old." ;? >

Displayed as years old for Peter is.

Traversing associative arrays

To traverse and output all the values of an associative array, you can use a foreach loop like this:

<? PHP $age=array("Bill" = "+", "Steve" = "Notoginseng", "Peter" and "+"); foreach ($ageas$x=$x _value) {   echo$x$x _value;    Echo "<br>";}? >

Key=bill, value=35
Key=steve, value=37
Key=peter, value=43

PHP-Multidimensional arrays

A multidimensional array refers to an array that contains one or more arrays.

PHP can understand multi-dimensional arrays of two or three, four, or five levels or more. However, arrays that are more than three levels deep are difficult for most people to manage.

Note: The dimensions of an array indicate the number of indexes you need to select an element.

    • For a two-dimensional array, you need two indexes to select the element
    • For three-dimensional arrays, you need three indexes to select the elements
PHP-two-D arrays

A two-d array is an array of arrays (three-dimensional arrays are arrays of arrays).

First, let's take a look at the following table:

Brand Stock Sales
Volvo 33 20
BMW 17 15
Saab 5 2
Land Rover 15 11

We can store the data in the table in a two-D array, like this:

$cars = Array ("Volvo", 22,18), Array ("BMW", 15,13), Array ("  Saab", 5,2), Array (  "land Rover ", 17,15)  );

Now this two-dimensional array contains four arrays, and it has two indexes (subscript): Rows and columns.

To access the elements in the $cars array, we must use two indexes (rows and columns):

Instance
<?PHP$cars=Array   (   Array("Volvo", 33,20),Array("BMW", 17,15),Array("Saab", 5,2),Array("Land Rover", 15,11)   ); Echo $cars[0] [0]. ": Inventory:".$cars[0] [1]. ", Sold:".$cars[0] [2]. ". <br> ";Echo $cars[1] [0]. ": Inventory:".$cars[1] [1]. ", Sold:".$cars[1] [2]. ". <br> ";Echo $cars[2] [0]. ": Inventory:".$cars[2] [1]. ", Sold:".$cars[2] [2]. ". <br> ";Echo $cars[3] [0]. ": Inventory:".$cars[3] [1]. ", Sold:".$cars[3] [2]. ". <br> ";?>

Results

Volvo: In Stock: 33, Sold: 20.
BMW: Stock: 17, sold: 15.
Saab: Stock: 5, Sold: 2.
Land Rover: Stock: 15, sold: 11.

We can also use another for loop in the for loop to get the elements in the $cars array (we still need to use two indexes):

Instance
<?PHP$cars=Array   (   Array("Volvo", 33,20),Array("BMW", 17,15),Array("Saab", 5,2),Array("Land Rover", 15,11)   );  for($row= 0;$row< 4;$row++) {   Echo"<p><b> Number of rows$row</b></p> "; Echo"<ul>";  for($col= 0;$col< 3;$col++) {     Echo"<li>".$cars[$row][$col]." </li> "; }   Echo"</ul>";}?>

Run results

Number of rows 0

    • Volvo
    • 33
    • 20

Number of rows 1

    • BMW
    • 17
    • 15

Number of rows 2

    • Saab
    • 5
    • 2

Number of rows 3

    • Land Rover
    • 15
    • 11

Each array of PHP collation collection

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.