Php array declaration, traversal, array global variables

Source: Internet
Author: User
Tags foreach arrays data structures explode http post php tutorial

Php Tutorial array declaration, traversal, array global variables

<? 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 '<pre> ';
Print_r ($ user );
Echo '</pre> ';

// 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

<? Php
/* 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.
 }
 
 


?>

<! -- *************************** -->
<? Php
// Directly use the passed value as a variable, which is useful when register_global = on is in the php. ini configuration file.
Echo $ username. "<br> ";
Echo $ email. "<br> ";
Echo $ page. "<br> ";
// The most stable value method
Echo $ _ GET ["username"]. "<br> ";
Echo $ _ GET ["email"]. "<br> ";
Echo $ _ GET ["page"]. "<br> ";
?>
<A href = "demo. php? Username = zhangsan & email = aaa@bbb.com & page = 45 "> this is a $ _ GET test </a>

<! -- ***************************** -->
<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); // cannot receive
Print_r ($ _ POST); // to receive
?>

<? Php
// $ _ ENV usage
Echo '<pre> ';
Print_r ($ _ ENV );
Echo '</pre> ';
// Display the current environment
// You can also traverse
?>

<? Php
// 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"]. "<br> ";
Echo $ GLOABLS ["B"]. "<br> ";
Echo $ GLOABLS ["c"]. "<br> ";
  
 }
?>


Array traversal

<? Php
/* 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]. "<br> ";
 }
 
// Use foreach
$ User = array (1, "zhangsan", 40, "nan ");
Foreach ($ user as $ val) // $ val is a custom variable
 {
Echo $ val. "<br>"; // The output has nothing to do with the subscript.
 }

Foreach ($ user as $ key => $ val) // $ val $ key is a custom variable
 {
Echo $ key. "=====>". $ val. "<br> ";
 }
 
// 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 '<table align = "center" width = "500" border = "1"> ';
Echo '<caption> Foreach ($ table as $ row)
 {
Echo '<tr> ';
Foreach ($ row as $ col)
  {
Echo '<td>'. $ col. '</td> ';
  }
Echo '</tr> ';
 }
Echo '</table> ';
}
 

// 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]. "<br>"; // displays the key (subscript) and value through 0, 1.
Echo $ arr ["key"]. "==>". $ arr ["value"]. "<br>"; // key and value are used to display key values.
 }

 

// Use the list () function
List ($ name, $ age, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ name. "<br> ";
Echo $ age. "<br> ";
Echo $ sex. "<br> ";
// Another method
List (, $ sex) = array ("zhangsan", 10, "nnnnn ");
Echo $ sex. "<br>"; // 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. "<br> ";
 }


// 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. "<br> ";
 }
// 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. "<br> ";
 } 
While (list ($ key, $ value) = each ($ user) // The loop jumps out directly because each () returns false to the last one.
 {
Echo $ key. "--->". $ value. "<br> ";
 } 
 
Echo current ($ user). "====>". key ($ user );
 

?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.