Php array definition tutorial _ PHP Tutorial

Source: Internet
Author: User
Tags array definition
Php array definition tutorial details. 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. PHP arrays are a type of array definition in php. There are several methods such as array (), or arr [] implements the 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 response

Use.
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.

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 created in PHP is defined using the array () structure, for example:

The code is as follows:


$ People = array ('name', 'sex', 'nation', 'brith ');

For how to display the values of each element in the array, we use an index starting from 0, and the index number is in square brackets after the variable name, such

The 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 array means that the keyword can be customized to replace the imintuitive

Digital indexes, such:

The code is as follows:


$ Doneles = array ('xm '=> 'name', 'xb' => 'sex', 'mz' => 'nation ', 'CS '=> 'birth ');
Echo $ doneles ['CS '];
?>

Use related arrays to make the output selection intuitive (no pre-calculation of index numbers is required and then output). use the defined keywords and values

"=>" Symbol definition.

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.

For example

The code is as follows:


$ People [0] = 'name ';
$ People [1] = 'sex ';
$ People [2] = 'Nation ';
$ People [3] = 'brith ';

Or

The code is as follows:


$ Les ['xm '] = 'name ';
$ Doneles ['xb '] = 'sex ';
$ Doneles ['mz'] = 'Nation ';
$ Les ['CS '] = 'birth ';

The size of the array varies dynamically according to the number of added elements.

The 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,

Struct (), or arr [] implements array definition. next I will give you a detailed introduction of various techniques for defining php arrays...

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.