There are several ways to define arrays in php, such as array(), or arr[] to implement array definition. Let me introduce to you in detail. Detailed explanation of various techniques of PHP array definition
PHP array is an important concept. It contains a large number of functions to facilitate people's development... Its arrays are now classified to facilitate query and application
Use.
Let’s talk about the definition of PHP array first... PHP array contains two items, key and value. The corresponding value can be obtained through key, and key can be
It is numerical and related, such as $array[0], $array[one]...
Create array
The array declaration in PHP is slightly different from that in other languages, but it can still be declared as one-dimensional, two-dimensional, three-dimensional and multi-dimensional, such as
$array[0] = 1,$array = array(1,2,3); One-dimensional array, including only three values, is a numeric array, and can be referenced with $array
[0] represents 1, you can omit the index when creating a numerical array
Create an array in PHP using the array() structure to define it, such as:
The code is as follows | Copy code | ||||
$people=array('name','sex','nation','brith'); |
代码如下 | 复制代码 |
:
|
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||
:
$people=array('name','sex','nation','birth');
?>
The output $people[2] shows nation (the first item of the index is counted from 0). In addition to supporting numerical index arrays, PHP also supports related arrays. The so-called related array means that you can customize keywords to replace unintuitive
The size of the array changes dynamically according to the number of elements added.
//index array $user[0]=1;//User serial number $user[1]="zhangsan";//Username $user[2]=10;//Age $user[3]="nan";//Gender echo ' '; <br> Print_r($user); <br> echo ''; //Associative array $user["id"]=1; $user["name"]="zhangsan"; $user["age"]=10; $user["sex"]; $user["age"]=90;//Assignment Echo $user["name"];//Output //Use array() to declare an array $user=array(1,"zhangsan",10,"nan"); //Use array() to declare an associative array $user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan"); //Declare a multi-dimensional array (multiple records) to save multiple user information records in a table $user=array( //Use $user[0] to call this line, such as calling the name in this record, $user[0][1] array(1,"zhangsan",10,"nan"), //Use $user[1] to call this line, such as calling the name in this record, $user[1][1] array(2,"lisi",20,"nv") ); //Array saves multiple tables, each table has multiple records $info=array( "user"=>array( array(1,"zhangsan",10,"nan"), array(2,"lisi",20,"nv") ), "score"=>array( array(1,90,80,70), array(2,60,40,70) ) ); Echo $info["score"][1][1];//Output 60, http: //www.bkjia.com/PHPjc/628676.html TechArticle There are several ways to define arrays in php, such as array(), or arr[] to implement array definition, as follows Let me introduce to my friends in detail various techniques about PHP array definition. Detailed explanation of PHP array is an important...
Related labels:
source:php.cn
Previous article:PHP Operator - PHP Basic Introductory Tutorial_PHP Tutorial
Next article:Why don’t some people add it at the end of their php code? >_PHP Tutorial
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|