Home > Article > Backend Development > Learn array declaration for newbies in PHP_PHP Tutorial
Array is a form of organizing several variables of the same type in an orderly manner for the convenience of processing in programming. These collections of similar data elements arranged in order are called arrays. The following introduces the array declaration in PHP.
1. Overview of arrays
1. The essence of arrays: manage and operate a set of variables, batch processing
2. Arrays are composite Type (can store multiple)
3. Arrays can store data of any length and any type of data
4. Arrays can complete the functions of other language data structures (linked lists , queue, stack, collection class)
2. Classification of arrays
There are multiple units in the array, (units are called elements)
Each element (subscript [key] and value)
When accessing an element individually, the element is accessed through the subscript (key)
1. One-dimensional array, two-dimensional array , a three-dimensional array. . . Multidimensional arrays (arrays of arrays, that is, other arrays stored in the array)
2. There are two types of arrays in PHP
Index arrays: indexes whose subscripts are sequential integers
Associative array: The subscript is a string as an index
There are only two subscripts (integer, string)
3. Multiple declaration methods for arrays
1. Directly assign value statements to array elements
If the index subscript is not given, the sequential indexing will start from 0
If the index subscript is given, The next one will be incremented by 1 starting from the largest
If the previous subscript appears later, if it is an assignment, the previous element will be reassigned
In mixed declarations, index and association do not affect each other. (Does not affect the declaration of index subscripts)
2. Use the array() function to declare
The default is an index array. If you specify a subscript for an associative array and an index array, use key => value , use " , " to separate multiple members
3. Use other function declarations
(1) Index array
<ol class="dp-c"> <li class="alt"><span><span class="vars">$user</span><span>[0]=1;</span><span class="comment">//用户序号 </span><span> </span></span></li> <li> <span class="vars">$user</span><span>[1]=</span><span class="string">"zhangsan"</span><span>;</span><span class="comment">//用户名 </span><span> </span> </li> <li class="alt"> <span class="vars">$user</span><span>[2]=10;</span><span class="comment">//年龄 </span><span> </span> </li> <li> <span class="vars">$user</span><span>[3]=</span><span class="string">"nan"</span><span>;</span><span class="comment">//性别 </span><span> </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'<pre class="brush:php;toolbar:false">'</span><span>; </span> </li> <li> <span>print_r(</span><span class="vars">$user</span><span>); </span> </li> <li class="alt"> <span class="func">echo</span><span> </span><span class="string">'';
(2) Associative array
<ol class="dp-c"> <li class="alt"><span><span><'php </span></span></li><li><span class="vars">$user</span><span>[</span><span class="string">"id"</span><span>]=1; </span></li><li class="alt"><span class="vars">$user</span><span>[</span><span class="string">"name"</span><span>]=</span><span class="string">"zhangsan"</span><span>; </span></li><li><span class="vars">$user</span><span>[</span><span class="string">"age"</span><span>]=10; </span></li><li class="alt"><span class="vars">$user</span><span>[</span><span class="string">"sex"</span><span>]; </span></li><li><span class="vars">$user</span><span>[</span><span class="string">"age"</span><span>]=90;</span><span class="comment">//赋值 </span><span> </span></li><li class="alt"><span class="func">echo</span><span> </span><span class="vars">$user</span><span>[</span><span class="string">"name"</span><span>];</span><span class="comment">//输出 </span><span> </span></li><li><span class="comment">//使用array()声明数组 </span><span> </span></li><li class="alt"><span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>); </span></li><li><span class="comment">//使用array()声明关联数组 </span><span> </span></li><li class="alt"><span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>(</span><span class="string">"id"</span><span>=>1,</span><span class="string">"name"</span><span>=></span><span class="string">"zhangsan"</span><span>,</span><span class="string">"age"</span><span>=>10,</span><span class="string">"sex"</span><span>=></span><span class="string">"nan"</span><span>); </span></span></li> <li> <span class="comment">//声明多维数组(多条记录),来保存一个表中的多条用户信息记录 </span><span> </span> </li> <li class="alt"> <span class="vars">$user</span><span>=</span><span class="keyword">array</span><span>( </span> </li> <li> <span class="comment">//用$user[0]调用这一行,比如调用这条记录中的姓名,$user[0][1] </span><span> </span> </li> <li class="alt"> <span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>), </span> </li> <li> <span class="comment">//用$user[1]调用这一行,比如调用这条记录中的姓名,$user[1][1] </span><span> </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,</span><span class="string">"lisi"</span><span>,20,</span><span class="string">"nv"</span><span>) </span> </li> <li><span>); </span></li> <li class="alt"> <span class="comment">//数组保存多个表,每个表有多条记录 </span><span> </span> </li> <li> <span class="vars">$info</span><span>=</span><span class="keyword">array</span><span>( </span> </li> <li class="alt"> <span class="string">"user"</span><span>=></span><span class="keyword">array</span><span>( </span> </li> <li> <span class="keyword">array</span><span>(1,</span><span class="string">"zhangsan"</span><span>,10,</span><span class="string">"nan"</span><span>), </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,</span><span class="string">"lisi"</span><span>,20,</span><span class="string">"nv"</span><span>) </span> </li> <li><span>), </span></li> <li class="alt"> <span class="string">"score"</span><span>=></span><span class="keyword">array</span><span>( </span> </li> <li> <span class="keyword">array</span><span>(1,90,80,70), </span> </li> <li class="alt"> <span class="keyword">array</span><span>(2,60,40,70) </span> </li> <li><span>) </span></li> <li class="alt"><span>); </span></li> <li> <span class="func">echo</span><span> </span><span class="vars">$info</span><span>[</span><span class="string">"score"</span><span>][1][1];</span><span class="comment">//输出60, </span><span> </span> </li> <li class="alt"><span>?> </span></li> </ol>
I hope that the introduction in this article can help you.