Php array declaration, traversal, array global variable _ PHP Tutorial

Source: Internet
Author: User
Php array declaration, traversal, array global variables. Php Tutorial array declaration, traversal, array global variable [php]? ** 1. Array Overview * 1. the essence of arrays: managing and operating a group of variables, batch processing * 2. array composite type (php Tutorial array declaration, traversal, array global variables can be used)


[Php]
/*
* 1. Array Overview
* 1. the essence of arrays: managing and operating a group of variables, batch processing
* 2. array composite type (multiple objects can be stored)
* 3. data of any length or type can be stored in an array
* 4. arrays can be used to complete data structures in other languages (linked lists, queues, stacks, and collection classes)
*
*
*
* 2. array classification
* The array contains multiple units (elements)
* Each element (subscript [key] and value)
* When accessing a single element, all elements are accessed by subscript (key ).
* 1. one-dimensional array, two-dimensional array, and three-dimensional array... Multi-dimensional array
* (The array contains other arrays)
* 2. PHP has two types of arrays.
* Index array: index where the subscript is an ordered integer
* Associated array: the subscript is a string as an index.
*
* Only these two subscripts (integers and strings) exist.
*
*
* 3. multiple array declaration methods
*
* 1. directly assign a value to an array element
* If the index subscript is not given, the index starts from 0.
* If an index subscript is given, the next index increases by 1 from the maximum.
* If the subscript appears next to it, if it is a value assignment, it is to re-assign the value to the previous element.
* When a hybrid declaration is made, the index and association do not affect each other (the Declaration of the index subject is not affected)
*
* 2. use the array () function declaration
* The default value is an index array.
* If you specify a subscript for the associated array and index array, use the key => value
* Use "," to separate multiple members
* 3. use other function declarations
*
*
*
*
*/
// Index the array
$ User [0] = 1; // user serial number
$ User [1] = "zhangsan"; // user name
$ User [2] = 10; // age
$ User [3] = "nan"; // gender
Echo'

'; 
print_r($user);
echo '
';
// Associate an array
$ User ["id"] = 1;
$ User ["name"] = "zhangsan ";
$ User ["age"] = 10;
$ User ["sex"];
$ User ["age"] = 90; // value assignment
Echo $ user ["name"]; // output
// Use array () to declare an array
$ User = array (1, "zhangsan", 10, "nan ");
// Declare the joined array using array ()
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
// Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
$ User = array (
// Call this line with $ user [0]. for example, call the name in this record, $ user [0] [1]
Array (1, "zhangsan", 10, "nan "),
// Call this line with $ user [1]. for example, call the name in this record, $ user [1] [1]
Array (2, "lisi", 20, "nv ")
);
// Multiple tables are saved in an array. Each table has multiple records.
$ Info = array (
"User" => array (
Array (1, "zhangsan", 10, "nan "),
Array (2, "lisi", 20, "nv ")
),
"Score" => array (
Array ),
Array (2, 60, 40, 70)
)

);
Echo $ info ["score"] [1] [1]; // output 60,
?>
Array Super global variable
/* Pre-defined array:
* Automatic global variable --- Super Global Array
*
* 1. contains data from WEB servers, clients, runtime environments, and user input.
* 2. These arrays are special.
* 3. These arrays can be used directly if they take effect automatically within the global range.
* 4. users cannot customize these arrays, but these arrays are operated in the same way as their own arrays.
* 5. You can directly use these arrays in functions.
*
* $ _ GET // variable submitted to the script through URL request
* $ _ POST // variables submitted to the script through the http post method
* $ _ REQUEST // variables submitted to the script through GET, POST, and COOKIE mechanisms
* $ _ FILES // variables submitted to the script by uploading FILES via the http post method
* $ _ COOKIE
* $ _ SESSION
* $ _ ENV // variables submitted to the script in the execution environment
* $ _ SERVER // The variable is set by the web server or directly associated with the execution environment of the current script.
* $ GLOBALS // as long as the variables are valid for the current script, the array key name is the name of the global script.
*
*
*/
// The Super Global array can be directly called within the function.
$ Arr = array (10, 20); // A general array
$ _ GET = array (50, 90); // A Super Global array
Function demo (){
Global $ arr; // you must include
Print_r ($ arr );
Print_r ($ _ GET); // You do not need to include a directly called ultra-global array.
}



?>

// Directly use the passed value as a variable, which is useful when register_global = on is in the php. ini configuration file.
Echo $ username ."
";
Echo $ email ."
";
Echo $ page ."
";
// The most stable value method
Echo $ _ GET ["username"]."
";
Echo $ _ GET ["email"]."
";
Echo $ _ GET ["page"]."
";
?>
This is a $ _ GET test


Print_r ($ _ GET); // cannot receive
Print_r ($ _ POST); // to receive
?>
// $ _ ENV usage
Echo'
'; 
print_r($_ENV);
echo'
';
// Display the current environment
// You can also traverse
?>
// Use the $ GLOBALS ultra-global array to call global variables within the function
$ A = 100;
$ B = 200;
$ C = 300;
Function demo ()
{
// Directly call global variables
Echo $ GLOBALS ["a"]."
";
Echo $ GLOABLS ["B"]."
";
Echo $ GLOABLS ["c"]."
";

}
?>

Array traversal
/* Array traversal
*
* 1. use the for statement to traverse the array cyclically
* 1. Other languages (only in this way)
* 2. this method is not the preferred method in PHP.
* 3. the array must be an index array and the subscript must be continuous.
* (Index array subscripts can be discontinuous, and arrays can also be associated arrays. These two types cannot be traversed)
*
* 2. use the foreach statement to traverse the array cyclically
* Foreacho (array variable as variable value ){
* // Loop body
*}
* 1. the number of loops is determined by the number of elements in the array.
* 2. Each loop assigns the elements in the array to the following variables respectively.
*
* Foreach (array variable as subscript variable => value variable ){
*}
*
*
* 3. while () list () each () combined loop traversal array
*
* Each () function:
* 1. an array is required as a parameter.
* 2. the returned result is an array.
* 3. the returned array is 0, 1, key, and value subscripts (fixed)
* 0 and key subscript are the keys of the elements in the current parameter array.
* 1 and value subscript are the values of the elements in the current parameter array.
* 4. by default, the current element is the first element.
* 5. after each execution, the current element is moved backward.
* 6. if the last element executes this function, false is returned.
* List () function:
* 1. list () = array (); assign an array to this function.
* 2. the number of elements in the array must be the same as the number of parameters in the list () function.
* 3. each element value in the array is assigned a value to each parameter in the list () function. list () converts each parameter to a variable.
* 4. list () can only accept indexed arrays
* 5. assign values to parameters in the subscript order of indexes.
*
*
*
*/
// For statement to traverse the array
$ User = array (1, "zhangsan", 40, "nan ");
For ($ I = 0; $ I <4; $ I ++)
{
Echo "$ user [{$ I}] =". $ user [$ I]."
";
}

// Use foreach
$ User = array (1, "zhangsan", 40, "nan ");
Foreach ($ user as $ val) // $ val is a custom variable
{
Echo $ val ."
"; // The output is irrelevant to the subscript.
}
Foreach ($ user as $ key => $ val) // $ val $ key is a custom variable
{
Echo $ key. "=====>". $ val ."
";
}

// Foreach traverses multi-dimensional arrays
$ 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, '123', 'AAA @ bbb.com '),
Array (2, '123', 'BBB @ ccc.com '),
Array (3, '123', '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) the default value is the value of the first element.
Print_r ($ );
$ B = each ($ user );
Print_r ($ B); // Array ([1] => zhangsan [value] => zhangsan [0] => name [key] => name) each time, traverse One

$ 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) value returned when no element exists
// Each () and while traversal
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While ($ arr = each ($ user ))
{
// Echo $ arr [0]. "===>". $ arr [1]."
"; // Display the key (subscript) and value through 0, 1
Echo $ arr ["key"]. "==>". $ arr ["value"]."
"; // Key and value are used to display the key value
}

// Use the list () function
List ($ name, $ age, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ name ."
";
Echo $ age ."
";
Echo $ sex ."
";
// Another method
List (, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ sex ."
"; // Only convert gender to variable
// Ip judgment
$ Ip = "192.168.1.128 ";
List (, $ d) = explode (".", $ ip); // explode is separated by. and an array is returned.
Echo $ d; // Retrieve 128
// List () 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) assign values to the parameters in the list according to the order of the index, so the values are 0 and 1.
Echo $ key. "--->". $ value;
// While list () each () combination
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While (list ($ key, $ value) = each ($ user ))
{
Echo $ key. "--->". $ value ."
";
}

// The solution is to display only once for multiple cycles
// Use the internal pointer control function of the array
// Next (array); move the array pointer to the next
// Prev (array); move the array pointer to the previous one
// Reset (array); move the array pointer to the first one (reset)
// End (array); the array pointer moves to the last one
// Current (array); obtains the value of the current element. the element pointed to by the index group pointer when the current element is present.
// Key (array); obtains the key value (subscript) of the current element)
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While (list ($ key, $ value) = each ($ user ))
{
Echo $ key. "--->". $ value ."
";
}
// Here, move the array pointer to the first loop below to output
// Reset ($ user)
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value ."
";
}
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value ."
";
}

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

/*
* 1. Array Overview
* 1. the essence of arrays: managing and operating a group of variables, batch processing
* 2. array composite type (multiple objects can be stored)
* 3. data of any length or type can be stored in an array
* 4. arrays can be used to complete data structures in other languages (linked lists, queues, stacks, and collection classes)
*
*
*
* 2. array classification
* The array contains multiple units (elements)
* Each element (subscript [key] and value)
* When accessing a single element, all elements are accessed by subscript (key ).
* 1. one-dimensional array, two-dimensional array, and three-dimensional array... Multi-dimensional array
* (The array contains other arrays)
* 2. PHP has two types of arrays.
* Index array: index where the subscript is an ordered integer
* Associated array: the subscript is a string as an index.
*
* Only these two subscripts (integers and strings) exist.
*
*
* 3. multiple array declaration methods
*
* 1. directly assign a value to an array element
* If the index subscript is not given, the index starts from 0.
* If an index subscript is given, the next index increases by 1 from the maximum.
* If the subscript appears next to it, if it is a value assignment, it is to re-assign the value to the previous element.
* When a hybrid declaration is made, the index and association do not affect each other (the Declaration of the index subject is not affected)
*
* 2. use the array () function declaration
* The default value is an index array.
* If you specify a subscript for the associated array and index array, use the key => value
* Use "," to separate multiple members
* 3. use other function declarations
*
*
*
*
*/
// Index the array
$ User [0] = 1; // user serial number
$ User [1] = "zhangsan"; // user name
$ User [2] = 10; // age
$ User [3] = "nan"; // gender
Echo'

';
print_r($user);
echo '
';
// Associate an array
$ User ["id"] = 1;
$ User ["name"] = "zhangsan ";
$ User ["age"] = 10;
$ User ["sex"];
$ User ["age"] = 90; // value assignment
Echo $ user ["name"]; // output
// Use array () to declare an array
$ User = array (1, "zhangsan", 10, "nan ");
// Declare the joined array using array ()
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
// Declare multi-dimensional arrays (multiple records) to save multiple user information records in a table
$ User = array (
// Call this line with $ user [0]. for example, call the name in this record, $ user [0] [1]
Array (1, "zhangsan", 10, "nan "),
// Call this line with $ user [1]. for example, call the name in this record, $ user [1] [1]
Array (2, "lisi", 20, "nv ")
);
// Multiple tables are saved in an array. Each table has multiple records.
$ Info = array (
"User" => array (
Array (1, "zhangsan", 10, "nan "),
Array (2, "lisi", 20, "nv ")
),
"Score" => array (
Array ),
Array (2, 60, 40, 70)
)

);
Echo $ info ["score"] [1] [1]; // output 60,
?>
Array Super global variable
/* Pre-defined array:
* Automatic global variable --- Super Global Array
*
* 1. contains data from WEB servers, clients, runtime environments, and user input.
* 2. These arrays are special.
* 3. These arrays can be used directly if they take effect automatically within the global range.
* 4. users cannot customize these arrays, but these arrays are operated in the same way as their own arrays.
* 5. You can directly use these arrays in functions.
*
* $ _ GET // variable submitted to the script through URL request
* $ _ POST // variables submitted to the script through the http post method
* $ _ REQUEST // variables submitted to the script through GET, POST, and COOKIE mechanisms
* $ _ FILES // variables submitted to the script by uploading FILES via the http post method
* $ _ COOKIE
* $ _ SESSION
* $ _ ENV // variables submitted to the script in the execution environment
* $ _ SERVER // The variable is set by the web server or directly associated with the execution environment of the current script.
* $ GLOBALS // as long as the variables are valid for the current script, the array key name is the name of the global script.
*
*
*/
// The Super Global array can be directly called within the function.
$ Arr = array (10, 20); // A general array
$ _ GET = array (50, 90); // A Super Global array
Function demo (){
Global $ arr; // you must include
Print_r ($ arr );
Print_r ($ _ GET); // You do not need to include a directly called ultra-global array.
}

?>

// Directly use the passed value as a variable, which is useful when register_global = on is in the php. ini configuration file.
Echo $ username ."
";
Echo $ email ."
";
Echo $ page ."
";
// The most stable value method
Echo $ _ GET ["username"]."
";
Echo $ _ GET ["email"]."
";
Echo $ _ GET ["page"]."
";
?>
This is a $ _ GET test


Print_r ($ _ GET); // cannot receive
Print_r ($ _ POST); // to receive
?>
// $ _ ENV usage
Echo'

';
print_r($_ENV);
echo'
';
// Display the current environment
// You can also traverse
?>
// Use the $ GLOBALS ultra-global array to call global variables within the function
$ A = 100;
$ B = 200;
$ C = 300;
Function demo ()
{
// Directly call global variables
Echo $ GLOBALS ["a"]."
";
Echo $ GLOABLS ["B"]."
";
Echo $ GLOABLS ["c"]."
";

}
?>

Array traversal
/* Array traversal
*
* 1. use the for statement to traverse the array cyclically
* 1. Other languages (only in this way)
* 2. this method is not the preferred method in PHP.
* 3. the array must be an index array and the subscript must be continuous.
* (Index array subscripts can be discontinuous, and arrays can also be associated arrays. These two types cannot be traversed)
*
* 2. use the foreach statement to traverse the array cyclically
* Foreacho (array variable as variable value ){
* // Loop body
*}
* 1. the number of loops is determined by the number of elements in the array.
* 2. Each loop assigns the elements in the array to the following variables respectively.
*
* Foreach (array variable as subscript variable => value variable ){
*}
*
*
* 3. while () list () each () combined loop traversal array
*
* Each () function:
* 1. an array is required as a parameter.
* 2. the returned result is an array.
* 3. the returned array is 0, 1, key, and value subscripts (fixed)
* 0 and key subscript are the keys of the elements in the current parameter array.
* 1 and value subscript are the values of the elements in the current parameter array.
* 4. by default, the current element is the first element.
* 5. after each execution, the current element is moved backward.
* 6. if the last element executes this function, false is returned.
* List () function:
* 1. list () = array (); assign an array to this function.
* 2. the number of elements in the array must be the same as the number of parameters in the list () function.
* 3. each element value in the array is assigned a value to each parameter in the list () function. list () converts each parameter to a variable.
* 4. list () can only accept indexed arrays
* 5. assign values to parameters in the subscript order of indexes.
*
*
*
*/
// For statement to traverse the array
$ User = array (1, "zhangsan", 40, "nan ");
For ($ I = 0; $ I <4; $ I ++)
{
Echo "$ user [{$ I}] =". $ user [$ I]."
";
}

// Use foreach
$ User = array (1, "zhangsan", 40, "nan ");
Foreach ($ user as $ val) // $ val is a custom variable
{
Echo $ val ."
"; // The output is irrelevant to the subscript.
}
Foreach ($ user as $ key => $ val) // $ val $ key is a custom variable
{
Echo $ key. "=====>". $ val ."
";
}

// Foreach traverses multi-dimensional arrays
$ 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, '123', 'AAA @ bbb.com '),
Array (2, '123', 'BBB @ ccc.com '),
Array (3, '123', '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) the default value is the value of the first element.
Print_r ($ );
$ B = each ($ user );
Print_r ($ B); // Array ([1] => zhangsan [value] => zhangsan [0] => name [key] => name) each time, traverse One

$ 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) value returned when no element exists
// Each () and while traversal
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While ($ arr = each ($ user ))
{
// Echo $ arr [0]. "===>". $ arr [1]."
"; // Display the key (subscript) and value through 0, 1
Echo $ arr ["key"]. "==>". $ arr ["value"]."
"; // Key and value are used to display the key value
}

// Use the list () function
List ($ name, $ age, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ name ."
";
Echo $ age ."
";
Echo $ sex ."
";
// Another method
List (, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ sex ."
"; // Only convert gender to variable
// Ip judgment
$ Ip = "192.168.1.128 ";
List (, $ d) = explode (".", $ ip); // explode is separated by. and an array is returned.
Echo $ d; // Retrieve 128
// List () 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) assign values to the parameters in the list according to the order of the index, so the values are 0 and 1.
Echo $ key. "--->". $ value;
// While list () each () combination
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While (list ($ key, $ value) = each ($ user ))
{
Echo $ key. "--->". $ value ."
";
}

// The solution is to display only once for multiple cycles
// Use the internal pointer control function of the array
// Next (array); move the array pointer to the next
// Prev (array); move the array pointer to the previous one
// Reset (array); move the array pointer to the first one (reset)
// End (array); the array pointer moves to the last one
// Current (array); obtains the value of the current element. the element pointed to by the index group pointer when the current element is present.
// Key (array); obtains the key value (subscript) of the current element)
$ User = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan ");
While (list ($ key, $ value) = each ($ user ))
{
Echo $ key. "--->". $ value ."
";
}
// Here, move the array pointer to the first loop below to output
// Reset ($ user)
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value ."
";
}
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
{
Echo $ key. "--->". $ value ."
";
}

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

Why [php]? /** 1. Array Overview * 1. array nature: manage and operate a group of variables, batch processing * 2. array composite type (yes...

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.