Explain the definition of an array of PHP and the method of creating arrays _php tips

Source: Internet
Author: User
Tags data structures numeric pear

Traditionally, an array is defined as a set of elements that have some common characteristics, the common features here include similarity (car models, baseball teams, fruit types, etc.) and types (for example, all elements are strings or integers), and each element is distinguished by a special identifier, which is called a health key. Notice the traditional last word in this sentence, because you can now discard this definition, which can include completely unrelated elements in the array structure. PHP goes further, and the elements in the array may not even belong to the same type. For example, an array might contain elements such as state names, postal codes, test scores, or poker.

Each entity contains two items: The key and the value mentioned earlier. You can obtain its corresponding value by querying the key. These keys can be numeric (numerical) or association (associative) health. Numeric keys have no real connection to values, they are just the positions of the values in the array. For example, an array contains the name of the fruit in alphabetical order, key 0 means Apple, and key 2 represents pear. Using the PHP syntax, the array is as follows:

$fruits = Array (
 "0" => "apple",
 "1" => "Banana" "
 2" => "pear"
 );

Using an array index, you can refer to the first element (Apple) as follows:

$fruits [0]

The numeric index group of PHP starts with position 0 instead of 1.

The difference is that the association key is related to the value, not the position of the value in the array. It is particularly convenient to map an array in an associative way by using numeric index values that are not row. For example, you might want to create an array that maps fruit abbreviations to fruit names, such as Ap/apple, Ba/banana, and Pe/pear. Using the PHP syntax, the array is as follows:

$fruits = Array (
 "AP" => "Apple", "
 BA" => "banana", "
 PE" => "pear"
 );

You can refer to Apple as follows:

$fruits ["AP"];

You can also create an array that contains an array, which is called a multidimensional array (multidimensional arrays). For example, you can use a multidimensional array to store information about the fruit. Using the PHP syntax, the array is as follows:

$fruits = Array ("
  Apple" =>array ("
 name" => "Apple",
 "color" => "Red"
 ),
 "Banana" => Array (
 "name" => "Banana",
 "color" => "yellow"
 )
);

You can then refer to Apple's color as follows:

$states ["Apple"] ["color"];

This returns the following values:

Red

You will naturally want to know how to traverse an array. PHP provides a lot of ways to traverse an array. Whichever method you use, remember that they all depend on an attribute called an array pointer. The array pointer is like a bookmark, telling you the location of the array you are checking. Instead of manipulating array pointers directly, you use built-in language features or functions to traverse an array. However, it is useful to understand this basic concept.


Arrays are one of the most important data structures in PHP, and arrays are very useful in PHP. Unlike the array implementations of many other languages, PHP does not need to specify its size when creating an array. In fact, because PHP is a loosely typed language, you don't even need to declare it before using an array, although there are no restrictions, PHP still provides a formal and informal array declaration method. Two methods have their merits and are worth learning. The two approaches are discussed separately below, and the informal approach is introduced first.

To refer to each element in the PHP array, you can use a pair of brackets to indicate. Because arrays do not have size limits, you can create arrays by simply establishing a reference, for example:

$fruits [0] = "apple";

You can then display the first element of the array $fruits as follows:

echo $fruits [0] = "apple";

Next, you can map the new values to the array index, adding additional values, as follows:

$fruits [1] = "banana";
$fruits [2] = "pear";

Interestingly, if the index value is considered to be an array index and is incremented, you can also omit the index value at creation time:

$fruits [] = "Apple";
$fruits [] = "banana";
$fruits [] = "pear";

Creating an associative array in this way is also simple, except that you must always use the key. The following instance creates an array that maps the fruit to its color:

$fruits ["apple"] = "red";
$fruits ["banana"] = "yellow";
$fruits ["pear"] = "yellow";

To create an array using array ()

The array () function accepts 0 or more elements as input and returns an array containing the revenue elements. The form is as follows:

Array array ([item1,[,item2 ... [, Itemn]]]

The following is an example of an indexed array using array ():

$fruits = Array ("Apple", "banana", "pear");

You can also use array () to create an associative array, as follows:

$fruits = Array (
 "AP" => "Apple", "
 BA" => "banana", "
 PE" => "pear"
 );

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.