How does PHP define arrays? Are there several ways to define arrays?

Source: Internet
Author: User
Tags array definition array to string php define
PHP Arraysis actually an ordered map. PHP ArraysA map is a type that associates values to the keys. This type is optimized in many ways, so it can be used as a real array, or as a list (vector), a hash (an implementation of a mapping), a dictionary, a collection, a stack, a queue, and more possibilities. Because PHP ArraysThe value of the element can also be another Array, the tree structure and multidimensional arrays are also allowed.

In general, the methods are defined as follows:

Method One:

$a =array (' A ', ' B ', ' C ');

The results of the operation are as follows.
Array
(
[0]=>a
[1]=>b
[2]=>c
[3]=>simon
)

Method Two:

$a =array (KEY1=>VALUE1,KEY2=>VALUE2,KEY3=>VALUE3);

Method Three:

$a [key1]=value1; $a [key2]=value2;

Method Four: Define an array by means of brackets []

PHP version 5.4 can be written later, the new array shorthand syntax.

PHP version 5.3 and previous versions do not accept this writing ...

$data = [' start_time ' = ' 123 ', ' end_time ' = ' 456 '];

Grammar

Defines an array of arrays ()

You can use the array () language structure to create a new array. It accepts any number of key (key) = = VALUES (value) pairs separated by commas.

Array (  key = value,  ...  ) Key (Key) is an integer or string string//value (value) can be any type of value

The comma after the last array element can be omitted. Typically used in a single-line array definition, such as a common array (1, 2) instead of an array (1, 2,). The last comma is usually reserved for multi-line array definitions, which makes it easier to add a new cell.

You can use the short array definition syntax since 5.4 to replace array () with [].


PHP arrays are simple and convenient to use, the same simple and convenient call, suitable for large and medium-sized data storage and call

Related recommendations:

Several ways to delete a specified value element in a PHP array

PHP array substitution function array_replace ()

Analysis of path method for array representation of PHP array to string

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.