PHP array declaration, traversal, array global variable usage summary _php tutorial

Source: Internet
Author: User
PHP Tutorial: Array declaration, traversal, array global variables

Copy CodeThe code is as follows:
/*
* First, an overview of arrays
* 1. The nature of the array: managing and manipulating a set of variables, batch processing
* 2. Compound type when array (multiple can be stored)
* 3. You can store any length of data in an array, or you can store any type of data
* 4. Array can complete the function of other language data structure (list, queue, Stack, collection Class)
*
*
*
* Second, the classification of the array
* There are multiple cells in the array (cells are called elements)
* each element (subscript [key] and value)
* When a single element is accessed, the element is accessed by subscript (key)
* 1. One-dimensional arrays, two-dimensional arrays, three-dimensional arrays ... Multidimensional arrays
* (Array of arrays, which is the existence of other arrays in the array)
* There are two types of arrays in 2.PHP
* Index array: is the index of an ordinal integer that is the subscript
* Associative array: Is the subscript is the string as the index
*
* Subscript (integer, string) only these two types of
*
*
* Three, array of multiple declaration methods
*
* 1. Direct array element Assignment declaration
* If index subscript is not given, it will be indexed sequentially from 0
* If index subscript is given, the next will increase by 1 from the largest start
* If the previous subscript appears, the assignment is to reassign the previous element
* When mixing declarations, indexes and associations do not affect each other (do not affect the declaration of index subscript)
*
* 2. Using the array () function declaration
* Default is indexed array
* If you specify subscripts for associative arrays and indexed arrays, use the key = = value
* use "," split between multiple members
* 3. Use a different function declaration
*
*
*
*
*/
Indexed array
$user [0]=1;//User number
$user [1]= "Zhangsan";//username
$user [2]=10;//Age
$user [3]= "Nan";//gender
Echo '


Echo '
';
Associative arrays
$user ["id"]=1;
$user ["Name"]= "Zhangsan";
$user ["Age"]=10;
$user ["Sex"];
$user ["Age"]=90;//assignment
echo $user ["name"];//output
Declaring an array using array ()
$user =array (1, "Zhangsan", Ten, "Nan");
Declaring an associative array using array ()
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
declaring multidimensional Arrays (multiple records) to hold multiple user information records in a table
$user =array (
Call this line with $user[0], such as calling the name in the record, $user [0][1]
Array (1, "Zhangsan", Ten, "Nan"),
Call this line with $user[1], such as calling the name in the record, $user [1][1]
Array (2, "Lisi", "NV")
);
Arrays save multiple tables with multiple records per table
$info =array (
"User" =>array (
Array (1, "Zhangsan", Ten, "Nan"),
Array (2, "Lisi", "NV")
),
"Score" =>array (
Array (1,90,80,70),
Array (2,60,40,70)
)

);
echo $info ["score"][1][1];//output 60,
?>
Array Super Global variables
/* pre-defined array:
* AUTO global variable---Hyper global array
*
* 1. Contains data from the Web server, client, run environment and user input
* 2. These arrays are more specific
* 3. The global scope automatically takes effect, you can use these arrays directly
* 4. Users cannot customize these arrays, but these arrays operate in the same way as array operations of their own definition
* 5. These arrays can be used directly in the function
*
* $_get//The variable submitted to the script via URL request
* $_post//variables submitted to the script via the HTTP POST method
* $_request//variables submitted to the script via get, post and cookie mechanism
* $_files//variables submitted to the script via HTTP POST method file upload
* $_cookie
* $_session
* $_env//Execution Environment commit to script variable
* $_server//variables are set by the Web server or are directly associated with the execution environment of the current script
* $GLOBALS//As long as the current script is valid variables are here, the array's key name is the name of the global script
*
*
*/
A hyper-global array can be called directly inside a function
$arr =array (10,20);//General array
$_get=array (50,90);///Hyper Global Array
Function Demo () {
Global $arr;//The call is to first include
Print_r ($arr);
Print_r ($_get);//directly calling a hyper-global array does not include
}



?>

Use the worth variable directly, which is useful when register_global=on in the php.ini configuration file.
echo $username. "
";
echo $email. "
";
echo $page. "
";
The most stable method of taking value
echo $_get["username"]. "
";
echo $_get["email"]. "
";
echo $_get["page"]. "
";
?>
This is a $_get test


Print_r ($_get);//cannot receive
Print_r ($_post);//To receive
?>
Use of $_env
Echo


Echo
';
Show Current environment
You can also traverse a single
?>
Calling global variables inside a function using a $globals hyper-global Array
$a = 100;
$b = 200;
$c = 300;
Function Demo ()
{
Calling global variables directly
echo $GLOBALS ["a"]. "
";
echo $GLOABLS ["B"]. "
";
echo $GLOABLS ["C"]. "
";

}
?>

Array traversal
/* Traversal of arrays
*
* 1. Loop through the array using the FOR statement
* 1. Other languages (only this way)
* 2.PHP in this way is not the preferred way
* 3. The array must be an indexed array, and the subscript must be contiguous.
* (index array subscript can be discontinuous, arrays also have associative arrays, these two cannot traverse)
*
* 2. Iterating through an array using a foreach statement
* Foreacho (array variable as variable value) {
*//Loop body
* }
* 1. The number of cycles is determined by the number of elements in the array
* 2. Each iteration assigns the elements in the array to the following variables, respectively
*
* foreach (array variable as subscript variable = = value variable) {
* }
*
*
* 3.while () list () each () combination loops through the array
*
* each () function:
* 1. Requires an array as a parameter
* 2. Returned is also an array
* 3. The returned array is 0,1,key,value four subscript (fixed)
* 0 and key subscript is the key of the current parameter array element
* 1 and value subscript is the value of the current parameter array element
* 4. The default current element is the first element
* 5. The current element is moved backwards after each execution
* 6. Returns False if the function is executed again to the last element
* List () function:
* 1. List () =array (); You need to assign an array to this function
* 2. The number of elements in the array to be the same as the number of parameters in the list () function
* 3. Each element value in the array is assigned a value of each parameter in the list () function, and list () converts each argument to a variable
* 4.list () can only accept indexed arrays
* 5. Assigning values to parameters in the order of index subscripts
*
*
*
*/
For statement Traversal array
$user =array (1, "Zhangsan", +, "Nan");
for ($i =0; $i <4; $i + +)
{
echo "$user [{$i}]=]. $user [$i]."
";
}

Using the Foreach
$user =array (1, "Zhangsan", +, "Nan");
foreach ($user as $val)//$val is a custom variable
{
echo $val. "
";//output is not related to subscript
}
foreach ($user as $key = + $val)//$val $key are custom variables
{
echo $key. " =====> ". $val."
";
}

foreach traversal multidimensional array
$info =array (
"User" =>array (
$user [0]
Array (1, "Zansan", Ten, "Nan"),
$user [1][1]
Array (2, "Lisi", +, "NV"),//$user [1]
$user [2]
Array (3, "Wangwu", +, "Nan")
),
"Score" =>array (
Array (1, 100, 90, 80),
Array (2, 99, 88, 11),
Array (3, 10, 50, 88)
),
"Connect" =>array (
Array (1, ' A ', ' aaa@bbb.com '),
Array (2, ' + ', ' bbb@ccc.com '),
Array (3, ' 119 ', ' ccc@ddd.com ')
)
);
foreach ($info as $tableName = $table)
{
Echo '










'; Echo ' '; foreach ($table as $row) {echo ' '; foreach ($row as $col) {echo ' '; } Echo ' '; } Echo '

'. $tableName. '

'. $col. '
';
}

Use of each ()
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
$a =each ($user);//array ([1] = 1 [value] + 1 [0] = + ID [key] + = ID) default is the value of the first element
Print_r ($a);
$b =each ($user);
Print_r ($b);//array ([1] = Zhangsan [value] = Zhangsan [0] = = name [key] + = name) Each time it is executed, a backward traversal

$c =each ($user);
Print_r ($c);//array ([1] = [value] = [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) when there are no elements, the value returned
each () Mate while traversal
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
while ($arr =each ($user))
{
echo $arr [0]. " ====> ". $arr [1]."
";//through 0, which shows the key (subscript) and the value
echo $arr ["Key"]. ===> ". $arr [" Value "]."
";//Key,value to display key values
}

Use of the list () function
List ($name, $age, $sex) =array ("Zhangsan", Ten, "nnnnn");
echo $name. "
";
echo $age. "
";
echo $sex. "
";
Another way to use
List (,, $sex) =array ("Zhangsan", Ten, "nnnnn");
echo $sex. "
";//convert gender to variable only
IP judgment
$ip = "192.168.1.128";
List (,,, $d) =explode (".", $ip);//explode. To separate and return an array
echo $d;//Remove 128
List () can only receive an example of an indexed array
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
List ($key, $value) =each ($user),//array ([1]=>1 [0]=>id) assigns a value to a parameter in the list in the order of index subscripts, so first 0 keys and then 1
echo $key. " ---> ". $value;
The while list () combination uses the
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
while (list ($key, $value) =each ($user))
{
echo $key. " ---> ". $value."
";
}

A workaround that displays multiple loops only once
Using the internal pointer control function of an array
Next (array), the array pointer moves to the next
Prev (array), the array pointer moves to the previous
Reset (array), the array pointer moves to the first (reset)
End (array), the array pointer moves to the last
Current (array); Gets the value of the current element, the element that the exponent group pointer points to when it is present.
Key (array); Gets the key value of the current element (subscript)
$user =array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "sex" = "Nan");
while (list ($key, $value) =each ($user))
{
echo $key. " ---> ". $value."
";
}
This is where you move the array pointer to the first of the following loops to output
Reset ($user)
while (list ($key, $value) =each ($user))//Because each () returns false to the last, so the loop jumps directly
{
echo $key. " ---> ". $value."
";
}
while (list ($key, $value) =each ($user))//Because each () returns false to the last, so the loop jumps directly
{
echo $key. " ---> ". $value."
";
}

Echo current ($user). " =====> ". Key ($user);
?>

http://www.bkjia.com/PHPjc/327468.html www.bkjia.com true http://www.bkjia.com/PHPjc/327468.html techarticle PHP Tutorial: Array declaration, traversal, array global variables copy the code code as follows:?/* * First, an overview of arrays * 1. The nature of arrays: managing and manipulating a set of variables, batch processing * 2. Array ...

  • Related Article

    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.