What are the arrays in PHP?

Source: Internet
Author: User
What is an array?

The registry is a large database. The key value is located at the end of the registry structure chain, similar to the file system file, containing the actual configuration information and data used by the current computer and application execution.

The array is the language structure of a key-value pair, similar to the hotel's room number, which is similar to what is stored in the hotel room.

Create an empty array: $arr =array ();

An array of PHP arrays can store multiple values in a single variable:

<?php$cars=array (    "Volvo",    "BMW", "    Toyota"    );    echo "I like". $cars [0]. ", " . $cars [1]. "and". $cars [2]. ".";? >

What is an array?

An array is a special variable that can store multiple values in a single variable.

If you have a list of items (for example: A list of car names), store them in a single variable, as follows:

<?php    $cars 1= "Volvo";    $cars 2= "BMW";    $cars 3= "Toyota";? >

However, what if you want to go through the group and find a specific one? What if the array has more than 3 items but 300?

The workaround is to create an array!

An array can store multiple values in a single variable, and you can access the values in them based on the key.

Creating an array in PHP

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

Array ();

In PHP, there are three types of arrays:

Numeric array-an array with a numeric ID key

Associative array-An array with the specified key, with each key associated with a value

Multidimensional arrays-An array that contains one or more arrays

PHP Numeric Array

Here are two ways to create a numeric array:

Auto-Assign ID key (ID key always starts from 0):

$cars =array ("Volvo", "BMW", "Toyota");//Manually Assign ID key: $cars [0]= "Volvo"; $cars [1]= "BMW"; $cars [2]= "Toyota";

The following instance creates an array of values named $cars, assigns an array of three elements, and then prints a section of text containing the values of the array:

Instance

<?php    $cars =array ("Volvo", "BMW", "Toyota"), echo "I like". $cars [0]. ", " . $cars [1]. "and". $cars [2]. ".";? >

Gets the length of the array-count () function

The count () function returns the length of the array (number of elements):

Instance

<?php    $cars =array ("Volvo", "BMW", "Toyota"); Echo count ($cars);? >

Iterating through an array of values

Traversing and printing all values in a numeric array, you can use the For loop as follows:

Instance

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

PHP Associative array

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

Here are two ways to create associative arrays:

$age =array ("Peter" and "+", "ben" = "Notoginseng", "Joe" = "" "), or: $age [' Peter ']= '; $age [' Ben ']= ' PNs '; $age [' Joe ']= "43";

You can then use the specified key in the script:

Instance

<?php     $age =array ("Peter" and "Ten", "Ben" and "Notoginseng", "Joe" and "the"); Echo "Peter is". $age [' Peter ']. "Years old."; ?>

Traversing associative arrays

Traversing and printing all the values in the associative array, you can use the Foreach loop as follows:

Instance

<?php     $age =array ("Peter" and "Ten", "Ben" = "Notoginseng", "Joe" and "" ");         foreach ($age as $x = + $x _value) {        echo "key=". $x. ", value=." $x _value;    echo "<br>";    } ?>


The foreach syntax structure provides an easy way to iterate through an array. foreach can only be applied to arrays and objects, if you try to apply a variable to another data type, or an uninitialized variable will emit an error message. There are two kinds of syntax:

foreach (array_expression as $value)
Statement
foreach (array_expression as $key = $value)
Statement

The first format iterates through the given array of array_expression. In each loop, the value of the current cell is assigned to the $value and the pointer inside the array is moved forward one step (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.

Related Article

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.