php learning array declaration

  • 2020-05-07 19:21:07
  • OfStack

 
<?php 
/* 
* 1.  Overview of arrays  
* 1. The nature of arrays: management and operations 1 Set of variables , Batch processing  
* 2. Array is a compound type ( You can store multiple ) 
* 3. Arrays can store data of any length or type  
* 4. Arrays perform the functions of data structures in other languages ( Linked list, queue, stack, collection class ) 
* 
* 
* 
* 2.  Classification of arrays  
*  There are multiple cells in the array, ( Units are called elements.)  
*  Each element ( The subscript [ key ] And the value ) 
*  When a single element is accessed, the index is used ( key ) To access elements  
* 1.1 Dimensional array, 2 Dimensional array, 3 Dimensional array... Multidimensional array  
* ( An array of arrays is an array in which there are other arrays ) 
* 2.PHP There are two types of arrays  
*  Indexed array: an index whose index index is a sequence of integers  
*  Associative array: the index is a string  
* 
*  The subscript ( Integer, string ) There are only two  
* 
* 
* 3.  Arrays are declared in many ways  
* 
* 1. Directly assign a value declaration to an array element  
*  If the index index is not given, the 0 Start order index  
*  If I give you the index index, down 1 I'm going to start with the largest 1 
*  If the preceding subscript appears, if it's an assignment, it reassigns the preceding element  
*  When mixing declarations, indexes and associations do not affect each other ( Does not affect the index index declaration ) 
* 
* 2. use array() Function declaration  
*  The default is an indexed array  
*  If you specify subscripts for associative and indexed arrays, use   key => value  
*  Used between multiple members " , " segmentation  
* 3. Use other function declarations  
* 
* 
* 
* 
*/ 
// The index array  
$user[0]=1;// User number  
$user[1]="zhangsan";// The user name  
$user[2]=10;// age  
$user[3]="nan";// gender  
echo '<pre>'; 
print_r($user); 
echo '</pre>'; 
// An associative array  
$user["id"]=1; 
$user["name"]="zhangsan"; 
$user["age"]=10; 
$user["sex"]; 
$user["age"]=90;// The assignment  
echo $user["name"];// The output  
// use array() Declare an array  
$user=array(1,"zhangsan",10,"nan"); 
// use array() Declare associative array  
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan"); 
// Declare multidimensional array ( Multiple records ) To save 1 Multiple records of user information in table  
$user=array( 
// with $user[0] Call it 1 Line, such as calling the name in this record ,$user[0][1] 
array(1,"zhangsan",10,"nan"), 
// with $user[1] Call it 1 Line, such as calling the name in this record ,$user[1][1] 
array(2,"lisi",20,"nv") 
); 
// An array holds multiple tables, each with 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];// The output 60, 
?> 

Related articles: