Several methods for defining PHP arrays

Source: Internet
Author: User
Tags array definition
Array Arrays

What is an array? An array is a special variable that can hold more than one value at a time. The array in PHP is actually an ordered map. A 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 the value of an array element can also be another array, the tree structure and the multidimensional array are also allowed.

In general, the methods are defined as follows:

Method One:

$a =array (1,2,4,5,6), <?php  $array =array (' A ', ' B ', ' C ');  $array []= ' Simon ';  Print_r ($array);  ? >

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 '];

Explaining these structures is beyond the scope of this manual, but at least one example is provided for each structure. For more information on these structures, it is recommended to refer to other works on this broad topic.

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 [].

Example #1 a simple array

<?php $array = Array (   "foo" = "Bar",   "bar" and "foo");   From PHP 5.4 $array = [   "foo" + "Bar",   "bar" = "foo",];?>

Key can be an integer or string. Value can be of any type.

In addition, key will have the following casts:

A string containing a valid integer value is converted to an integral type. For example, the key name "8" will actually be stored as 8. However, "08" does not cast because it is not a valid decimal value.

Floating-point numbers are also converted to integers, meaning that their fractional parts are removed. For example, key name 8.7 will actually be stored as 8.

Boolean values are also converted to integral types. That is, the key name true is actually stored as 1 and the key name false is stored as 0.

Null is converted to an empty string, that is, the key name null is actually stored as "".

Arrays and objects cannot be used as key names. Insisting on doing so will result in a warning: illegal offset type.

If more than one cell in the array definition uses the same key name, only the last one is used, and the previous is overwritten.

Example #2 type coercion and overrides example

<?php $array = Array (   1 =  "A",   "1" = "B",   1.5 = "C",   true = "D",); Var_dump ($arra y);?>

The above routines will output:

Array (1) {  [1]=>  string (1) "D"}

All of the key names in the previous example are cast to 1, each new cell overwrites the previous value, and the last remaining is only one "D".

PHP arrays can contain both integer and string key names, because PHP does not actually differentiate between indexed and associative arrays.

If no key name is specified for the given value, the current largest integer index value is taken, and the new key name is the value plus one. If the specified key name already has a value, the value is overwritten.

Example #3 mixed Integer and string key names

<?php $array = Array (   "foo" = "Bar",   "bar" = "foo",  -100, -100 = +   ,); var_ Dump ($array);?>

The above routines will output:

Array (4) {

["foo"]=>

String (3) "Bar"

["Bar"]=>

String (3) "Foo"

[100]=>

Int (-100)

[-100]=>

Int (100)

}

Key is optional. If not specified, PHP will automatically use the maximum integer key name previously used plus 1 as the new key name.

Example #4 an indexed array without a key name

<?php $array = Array ("foo", "Bar", "Hallo", "World"); Var_dump ($array);?>

The above routines will output:

Array (4) {

[0]=>

String (3) "Foo"

[1]=>

String (3) "Bar"

[2]=>

String (5) "Hallo"

[3]=>

String (5) "World"

}

You can also specify key names for some cells and empty them for others:

Example #5 Specify key names only for partial cells

<?php $array = Array (      "a",      "B",   6 = "C",      "D",); Var_dump ($array);?>

The above routines will output:

Array (4) {

[0]=>

String (1) "a"

[1]=>

String (1) "B"

[6]=>

String (1) "C"

[7]=>

String (1) "D"

}

You can see that the last value "D" is automatically assigned the key name 7. This is due to the previous maximum integer key name is 6.

Accessing array cells using square bracket syntax ¶

Array cells can be accessed through the Array[key] syntax.

Example #6 access to array cells

<?php $array = Array (   "foo" = "Bar",   "   Multi" = "      dimensional" = Array (        "Array" = "foo"))   ;   Var_dump ($array ["foo"]); Var_dump ($array [42]); Var_dump ($array ["Multi"] ["dimensional"] ["array"]);?>

The above routines will output:

String (3) "Bar"

Int (24)

String (3) "Foo"

Note:

Square brackets and braces can be used interchangeably to access array cells (for example $array [42] and $array {42} have the same effect in the previous example).

The result of a function or method call can be referenced indirectly from PHP 5.4 using an array. Only one temporary variable can be passed before.

An array prototype can be referenced indirectly from PHP 5.5 using an array.

Example #7 Array Indirect reference

<?php function GetArray () {   return Array (1, 2, 3);}   On PHP 5.4 $secondElement = GetArray () [1];   Previously $tmp = GetArray (); $secondElement = $tmp [1];   Or list (, $secondElement) = GetArray ();?>

Note:

Attempting to access an undefined array key name is the same as accessing any undefined variable: it causes the E_notice level error message and the result is NULL.

More questions about PHP arrays can be found on the relevant web site: Http://www.php.cn/search/PHP array. html

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.