Several methods of array definition in PHP

  • 2020-08-22 21:56:56
  • OfStack

Array array

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

1 Generally speaking, the definition methods are as follows:

Method 1:


$a=array(1,2,4,5,6);

<?php 
$array=array('a','b','c'); 
$array[]='simon'; 
print_r($array); 
?>

The results of the run are shown below.
Array
(
[0]= > a
[1]= > b
[2]= > c
[3]= > simon
)

Method 2:


$a=array(key1=>value1,key2=>value2,key3=>value3);

Method 3:


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

Method 4: Define an array through brackets []

php 5.4 after the version can be written like this, the new array abbreviation syntax.

php 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 1 example of each structure will be provided. For more information on these structures, it is recommended to refer to other works on this broad topic.

grammar

Define array array()

You can create a new array using the array() language structure. It accepts any number of comma-separated keys (key) = > Value (value) pair.

array( key = > value
, ...
)
The // key (key) is an integer integer or a string string
The // value (value) can be any type of value
Commas after the last 1 array element can be omitted. It is commonly used in single-row array definitions, for example array(1, 2) rather than array(1, 2,). The definition of a multi-row array is usually left with the last comma, which makes it easier to add a new cell.

From 5.4 onwards you can define the syntax using a short array, replacing array() with [].

Example #1 1 simple array


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

//  Since the  PHP 5.4  since 
$array = [
  "foo" => "bar",
  "bar" => "foo",
];
?>

key can be integer or string. value can be of any type.

In addition, key will have the following cast:

A string containing a valid integer value is converted to an integer. 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 decimal parts are omitted. For example, the key name 8.7 will actually be stored as 8.
Boolean values are also converted to integers. So the key name true will actually be stored as 1 and the key name false will be stored as 0.
Null will be converted to an empty string, meaning the key name null will actually be stored as "".
Arrays and objects cannot be used as key names. Doing this consistently results in a warning: Illegal offset type.
If more than one cell in an array definition USES the same key name, only the last one is used, and the previous one is overridden.

Example of Example #2 type enforcement and overwrite


<?php
$array = array(
  1  => "a",
  "1" => "b",
  1.5 => "c",
  true => "d",
);
var_dump($array);
?>

The above routine will output:

array(1) {
[1]= >
string(1) "d"
}
In the example above, all key names are cast to 1, so each new cell overwrites the value of the previous one, leaving only 1 "d".

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

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

Example #3 mixes the integer and string key names


<?php
$array = array(
  "foo" => "bar",
  "bar" => "foo",
  100  => -100,
  -100 => 100,
);
var_dump($array);
?>

The above routine 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 has no indexed array of key names


<?php
$array = array("foo", "bar", "hallo", "world");
var_dump($array);
?>

The above routine will output:

array(4) {
[0]= >
string(3) "foo"
[1]= >
string(3) "bar"
[2]= >
string(5) "hallo"
[3]= >
string(5) "world"
}
You can also specify a key name for some cells and leave others empty:

Example #5 specifies the key name only for some cells


<?php
$array = array(
     "a",
     "b",
  6 => "c",
     "d",
);
var_dump($array);
?>

The above routine 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 due to the fact that the largest integer key before was 6.

Use square bracket syntax to access array element ¶

Array elements can be accessed through the array[key] syntax.

Example #6 accesses array cells


<?php 
$array=array('a','b','c'); 
$array[]='simon'; 
print_r($array); 
?>
0

The above routine will output:

string(3) "bar"
int(24)
string(3) "foo"
Note:
Square brackets and curly braces can be used interchangeably to access array elements (for example, $array[42] and $array{42} have the same effect in the above example).
The result of a function or method call can be referenced indirectly with an array starting from PHP 5.4. You can only pass 1 temporary variable before.

From PHP 5.5 you can refer to an array prototype indirectly using an array.

Example #7 array indirect reference


<?php 
$array=array('a','b','c'); 
$array[]='simon'; 
print_r($array); 
?>
1

Note:
Trying to access an undefined array key name is the same as accessing any undefined variable 1: It results in an E_NOTICE level error, which results in NULL.

More content can refer to this article: http: / / www php. net manual/zh/language types. array. php


Related articles: