In php, arrays can be defined in several ways, such as array () or arr, next I will give you a detailed introduction of various techniques for defining php arrays.
In php, array definition has several methods, such as array () or arr [], to implement array definition. next I will give you a detailed explanation of various techniques for defining php arrays.
PHP array is an important concept. it contains a large number of functions for people's development... Now we classify its arrays to facilitate query and application.
Let's talk about the definition of PHP array... The PHP array contains two items: key and value. you can use the key to obtain the corresponding value. The key can be a value and associated, for example, $ array [0]. $ array [one]…
Create an array
The array declaration in PHP is a little different from that in other languages, but it can also be declared as one-dimensional, two-dimensional, three-dimensional and multi-dimensional, such
$ Array [0] = 1, $ array = array (, 3); one-dimensional array, which contains only three values and belongs to a numeric array. $ array is available for reference, [0] indicates 1. indexes can be omitted when a value array is created.
The array () structure is used to create an array in PHP. the instance code is as follows:
$ People = array ('name', 'sex', 'nation', 'brith ');
To display the values of each element in the array, we use an index starting from 0. The index number is in square brackets after the variable name. The example code is as follows:
-
- $ People = array ('name', 'sex', 'nation', 'birth ');
- Echo $ people [2];
- ?>
The output $ people [2] indicates that nation is displayed (the first index item is counted from 0 ). in addition to numeric index arrays, PHP also supports related arrays. The so-called related arrays are non-intuitive replace with custom keywords.
Code:
-
- $ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
- Echo $ doneles ['CS '];
- ?>
The related array is used to make the output selection intuitive (no pre-calculation of index numbers is required and then output). the defined keywords and values are defined using the "=>" symbol.
Based on the two display methods of PHP array elements, you can also create numbers automatically without the need for array () declaration and initialization like Variables. the instance code is as follows:
- $ People [0] = 'name ';
- $ People [1] = 'sex ';
- $ People [2] = 'Nation ';
- $ People [3] = 'brith ';
- // Or
- // Copy the following code:
- $ Les ['xm '] = 'name ';
- $ Doneles ['xb '] = 'sex ';
- $ Doneles ['mz'] = 'Nation ';
- $ Les ['CS '] = 'birth ';
The size of the array varies dynamically based on the number of elements added. The instance code is as follows:
- // Index the array
- $ User [0] = 1; // user serial number
- $ User [1] = "zhangsan"; // user name
- $ User [2] = 10; // age
- $ User [3] = "nan"; // gender
- Echo'
';
- Print_r ($ user );
- Echo'';
- // Associate an array
- $ User ["id"] = 1;
- $ User ["name"] = "zhangsan ";
- $ User ["age"] = 10;
- $ User ["sex"];
- $ User ["age"] = 90; // value assignment
- Echo $ user ["name"]; // output
- // Use array () to declare an array
- $ User = array (1, "zhangsan", 10, "nan ");
- // Declare the joined array using array ()
- $ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
- // Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
- $ User = array (
- // Call this line with $ user [0]. for example, call the name in this record, $ user [0] [1]
- Array (1, "zhangsan", 10, "nan "),
- // Call this line with $ user [1]. for example, call the name in this record, $ user [1] [1]
- Array (2, "lisi", 20, "nv ")
- );
- // Multiple tables are saved in an array. Each table has multiple records.
- $ Info = array (
- "User" => array (
- Array (1, "zhangsan", 10, "nan "),
- Array (2, "lisi", 20, "nv ")
- ),
- "Score" => array (
- Array ),
- Array (2, 60, 40, 70)
- )
- );
- Echo $ info ["score"] [1] [1]; // output 60,