PHP

php array declaration traversal array global variables using summary


php tutorial: Array declaration, traversal, array global variables


<?
/*
* 1.  An overview of arrays
* 1. The nature of arrays: management and manipulation 1 Set of variables , Batch processing
* 2. Array is a compound type ( Can store more than one )
* 3. An array can store data of any length or of any type
* 4. Arrays can perform the functions of other language data structures ( Linked list, queue, stack, collection class )
*
*
*
* 2.  Array classification
*  There are multiple cells in an array, ( Units are called elements)
*  Each element ( The subscript [ key ] And the value )
*  When you access a single element, it's always by subscript ( key ) To access the element
* 1.1 Dimensional array, 2 Dimensional array, 3 Dimensional array... Multidimensional array
* ( An array of arrays is an array that contains other arrays )
* 2.PHP There are two kinds of arrays
*  Index array: an index whose index is a sequential integer
*  Associative array: where the index is a string as an index
*
*  The subscript ( Integer, string ) These are the only two
*
*
* 3.  Arrays are declared in a variety of ways
*
* 1. Directly assign a value declaration to an array element
*  If the index subscript is not given, it will be given 0 Initial sequential index
*  If I give you index subscript, theta 1 I'm going to start with the largest 1
*  If the preceding index appears later, if it is an assignment, then the previous element is re-assigned
*  When mixing declarations, indexes and associations do not affect each other ( Does not affect the declaration of index subscripts )
*
* 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 arrays ( Multiple records ) To save 1 Multiple user information records in a 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")
);
// Arrays hold 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,
?>
 Array super global variables
<?php
/*  Predefined array:
*  Automatic global variable --- Superglobal array
*
* 1. Contains sources from WEB Server, client, runtime environment, and user input data
* 2. These arrays are special
* 3. Automatically takes effect globally, and you can use these arrays directly
* 4. The user cannot customize these arrays, but the way they operate and the way they define their own array operations 1 sample
* 5. You can use these arrays directly in a function
*
* $_GET // through URL The variable to which the request is submitted to the script
* $_POST // through HTTP POST  Method to submit to the script
* $_REQUEST // through GET , POST and COOKIE Mechanism to submit   Variables to the script
* $_FILES // through http post Method file uploaded and submitted to the script variable
* $_COOKIE
* $_SESSION
* $_ENV // Variables that the execution environment commits to the script
* $_SERVER // Variables by WEB Set by the server or directly associated with the execution environment of the current script
* $GLOBALS // As long as the current script is valid variables are here, the array key name is the name of the global script
*
*
*/
// The super global array can be called directly inside the function
$arr=array(10,20);//1 As an array
$_GET=array(50,90);// Superglobal array
function demo(){
global $arr;// A call to a global variable must first include
print_r($arr);
print_r($_GET);// Call the super global array directly without containing it
}



?>
<!-- ********** Page by value get request *************** -->
<?php
// I'm just going to use the value as a variable , when php.ini In the configuration file register_global=on When useful.
echo $username."<br>";
echo $email."<br>";
echo $page."<br>";
// The most stable way to evaluate it
echo $_GET["username"]."<br>";
echo $_GET["email"]."<br>";
echo $_GET["page"]."<br>";
?>
<a href="demo.php?username=zhangsan&[email protected]&page=45">this is a $_GET test</a>
<!-- *********** Page by value post request **************** -->
<form action="demo.php" method="post">
username:<input type="text" name="uname" /> <br/>
password:<input type="password" name="pass" /> <br/>
<input type="submit" value="login" /> <br />
</form>
<?php
print_r($_GET);// Can't receive
print_r($_POST);// In order to receive
?>
<?php
//$_ENV The use of
echo'<pre>';
print_r($_ENV);
echo'</pre>';
// Display the current environment
//  It can also be traversed individually
?>
<?php
// using $GLOBALS A super global array calls a global variable within a function
$a=100;
$b=200;
$c=300;
function demo()
{
// Call the global variable directly
echo $GLOBALS["a"]."<br>";
echo $GLOABLS["b"]."<br>";
echo $GLOABLS["c"]."<br>";

}
?>

 Array traversal
<?php
/*  Traversal of an array
*
* 1. use for Statement loops through groups
* 1. Other languages ( Only this 1 Kind of way )
* 2.PHP This is not the preferred approach
* 3. Arrays must be indexed arrays, and subscripts must be contiguous.
* ( Index array subscripts can be discontinuous , Arrays have associative arrays , These two cannot be traversed )
*
* 2. use foreach Statement loops through groups
* foreacho( An array variable  as  A variable's value ){
* // The loop body
* }
* 1. The number of loops is determined by the number of elements in the array
* 2. every 1 Each loop assigns each element of the array to a subsequent variable
*
* foreach( An array variable  as  The subscript variables =>  Values of a variable ){
* }
*
*
* 3.while() list() each()  The combination loop iterates through the groups
*
* each() Function:
* 1. Need to be 1 An array as a parameter
* 2. The return is the same 1 An array
* 3. The array returned is 0,1,key,value4 A subscript ( fixed )
* 0 and key The subscript is the key of the current parameter array element
* 1 and value The subscript is the value of the current parameter array element
* 4. The default current element is the first 1 An element
* 5. Each time 1 The current element is then moved backwards
* 6. Returns if the function is executed at the last element false
* list() Function:
* 1. list()=array(); You need to 1 An array is assigned to this function
* 2. The number of elements in the array list() The function has the same number of arguments
* 3. Each element in the array is assigned a value list() Each parameter in the function ,list() Turn each parameter into a variable
* 4.list() Only indexed arrays can be accepted
* 5. Assign values to parameters in the order of index subscripts
*
*
*
*/
//for Statement iterates through groups
$user=array(1,"zhangsan",40,"nan");
for($i=0;$i<4;$i++)
{
echo"$user[{$i}]=".$user[$i]."<br>";
}

// use foreach
$user=array(1,"zhangsan",40,"nan");
foreach($user as $val)//$val Is a custom variable
{
echo $val."<br>";// The output is independent of the subscript
}
foreach($user as $key=>$val)//$val $key  These are custom variables
{
echo $key."=====>".$val."<br>";
}

//foreach Traverse a multidimensional array
$info=array(
"user"=>array(
//$user[0]
array(1, "zansan", 10, "nan"),
//$user[1][1]
array(2, "lisi", 20, "nv"), //$user[1]
//$user[2]
array(3, "wangwu", 30, "nan")
),
"score"=>array(
array(1, 100, 90, 80),
array(2, 99, 88, 11),
array(3, 10, 50, 88)
),
"connect"=>array(
array(1, '110', '[email protected]'),
array(2, '120', '[email protected]'),
array(3, '119', '[email protected]')
)
);
foreach($info as $tableName=>$table)
{
echo '<table align="center" width="500" border="1">';
echo '<caption><h1>'.$tableName.'</h1></caption>';
foreach($table as $row)
{
echo '<tr>';
foreach($row as $col)
{
echo '<td>'.$col.'</td>';
}
echo '</tr>';
}
echo '</table>';
}

//each() The use of
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
$a=each($user);//Array ( [1] => 1 [value] => 1 [0] => id [key] => id )  The default is the first 1 The values of the elements
print_r($a);
$b=each($user);
print_r($b);//Array ( [1] => zhangsan [value] => zhangsan [0] => name [key] => name )  Each time 1 Times, traversing backwards 1 a

$c=each($user);
print_r($c);//Array ( [1] => 10 [value] => 10 [0] => age [key] => age )
$d=each($user);
print_r($d);//Array ( [1] => nan [value] => nan [0] => sex [key] => sex )
$e=each($user);
var_dump($e);//bool(false)  The value returned when there are no elements
//each() Cooperate with while traverse
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while($arr=each($user))
{
//echo $arr[0]."====>".$arr[1]."<br>";// through 0,1  To display the   key ( The subscript )  and   value
echo $arr["key"]."===>".$arr["value"]."<br>";// through key,value  To display the   key   value
}

//list() Use of functions
list($name,$age,$sex)=array("zhangsan",10,"nnnnn");
echo $name."<br>";
echo $age."<br>";
echo $sex."<br>";
// On the other 1 Method of use
list(,,$sex)=array("zhangsan",10,"nnnnn");
echo $sex."<br>";// Just convert gender into variables
//ip judge
$ip="192.168.1.128";
list(,,,$d)=explode(".",$ip);//explode Said with  .  To separate and return 1 An array
echo $d;// Take out the 128
//list() Examples that can only receive indexed arrays
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
list($key,$value)=each($user);//Array( [1]=>1 [0]=>id)  In the order of index subscripts list , so first of all  0 key   And then the  1 value
echo $key."--->".$value;
//while list() each()  Use a combination of
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."<br>";
}

// Multiple loops are shown only 1 The second solution
// Use the array's internal pointer control function
//next( An array of ); The array pointer moves down 1 a
//prev( An array of ); The array pointer moves up 1 a
//reset( An array of ); The array pointer moves to row 1 a ( reset )
//end( An array of ); The array pointer moves to the end 1 a
//current( An array of ); Gets the value of the current element , The element to which the index group pointer points when the current element is.
//key( An array of ); Gets the key value of the current element ( The subscript )
$user=array("id"=>1,"name"=>"zhangsan","age"=>10,"sex"=>"nan");
while(list($key,$value)=each($user))
{
echo $key."--->".$value."<br>";
}
// Here we move the array pointer to the first 1 The following loops can be output
//reset($user)
while(list($key,$value)=each($user))// because each() In the end 1 A return false So the loop just jumps out
{
echo $key."--->".$value."<br>";
}
while(list($key,$value)=each($user))// because each() In the end 1 A return false So the loop just jumps out
{
echo $key."--->".$value."<br>";
}

echo current($user)."=====>".key($user);
?>