Several methods of array definition in PHP _php tips

Source: Internet
Author: User
Tags array definition

Array arrays

The array in PHP is actually an ordered map. A mapping is a type that associates values to the keys. This type is optimized in many ways, so you can think of it as a real array, or a list (vector), a hash list (an implementation of a map), a dictionary, a collection, a stack, a queue, and more possibilities. Because the value of an array element can also be another array, a tree structure and a multidimensional array are allowed.

Generally, you define methods 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 shown below.
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 with brackets []

PHP version 5.4 can be so 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 '
];

Interpreting these constructs is beyond the scope of this manual, but provides at least one example for each structure. To get more information on these structures, it is advisable to refer to other books on this broad subject.

Grammar

Defines an array of arrays ()

You can use the array () language structure to create a new array. It accepts any number of comma-separated key (key) => values (value) pairs.

Array (key => value
, ...
)
Keys (key) is an integer or string
Values (value) can be any type of value
The comma after the last array cell can be omitted. Typically used in a single-line array definition, such as a common array (1, 2) rather than an array (1, 2,). A multiline array definition usually retains the last comma, which makes it easier to add a new cell.

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

Example #1 a simple array

<?php
$array = Array (
  "foo" => "Bar",
  "bar" => "foo"
);

From PHP 5.4
$array = [
  "foo" => "Bar",
  "bar" => "foo",
];
? >

The key can be either an integer or a string. Value can be any type.

In addition, the 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" is actually 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 the fractional part of the float is taken out. For example, the key name 8.7 is actually stored as 8.
The Boolean value is also converted to an integral type. That is, the key name true will actually be stored as 1 and the key name false will be stored as 0.
Null is converted to an empty string, where the key 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 multiple cells in the array definition use the same key name, only the last one is used, and the previous is overwritten.

Example #2 type coercion and overwrite example

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

The above routines will output:

Array (1) {
[1]=>
String (1) "D"
}
All of the key names in the example above 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 arrays and associative arrays.

If a key name is not 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 => 100,< c8/>);
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 automatically uses 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 only some of the cells and other vacancies:

Example #5 Assign key names to only a subset of the 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 given the key name 7. This is because the previous largest integer key name is 6.

Accessing array cells with brackets syntax ¶

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

Example #6 Access Array cells

<?php
$array = Array (
  "foo" => "Bar",  =>,
  "multi" => Array (
     "dimensional" = > Array (
       "array" => "foo"))
;

Var_dump ($array ["foo"]);
Var_dump ($array [a]);
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.

From PHP 5.5 You can indirectly reference an array prototype with 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.

For more information, refer to this article: http://www.php.net/manual/zh/language.types.array.php

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.