Php array declaration, traversal, array global variables use summary_php basics-php Manual

Source: Internet
Author: User
The essence of arrays: manages and operates a group of variables and processes them in batches. The following describes the array classification, array classification, and usage instructions. Php Tutorial: Array declaration, traversal, array global variables

 Value * Use "," to separate multiple members * 3. use other function declaration ***** // index array $ user [0] = 1; // user serial number $ user [1] = "zhangsan "; // username $ user [2] = 10; // Age $ user [3] = "nan"; // gender echo'
'; print_r($user); echo '
'; // Associated array $ user ["id"] = 1; $ user ["name"] = "zhangsan"; $ user ["age"] = 10; $ user ["sex"]; $ user ["age"] = 90; // value echo $ user ["name"]; // output // Use array () declare an array $ user = array (1, "zhangsan", 10, "nan"); // Use array () declare join array $ user = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan"); // declare a multi-dimensional array (multiple records ), to save multiple user information records in a table $ user = array (// call this row 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], such as calling this line Name in the record, $ user [1] [1] array (2, "lisi", 20, "nv"); // The array stores multiple tables, each table has multiple records $ info = array ("user" => array (1, "zhangsan", 10, "nan"), array (2, "lisi", 20, "nv"), "score" => array (,), array ))); echo $ info ["score"] [1] [1]; // output 60,?> Array Super global variable "; 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 ($ _ ENV); echo'
'; // Display the current environment. // Can a single traversal be performed?> "; Echo $ GLOABLS [" B "]."
"; Echo $ GLOABLS [" c "]."
";}?> Array traversal Value variable) {*} *** 3. while () list () each () combined loop traversal array ** each () function: * 1. an array is required as the 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 again, false * list () function is returned: * 1. list () = array (); you need to assign an array to this function * 2. number of elements in the array, which 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, and list () converts each parameter to the variable * 4. list () can only accept index arrays * 5. assign a value to the parameter ***** in the subscript order of the index // 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 the custom variable {echo $ val."
"; // The output has nothing to do with the subscript} foreach ($ user as $ key => $ val) // $ val $ key is a custom variable {echo $ key. "====> ". $ val."
";}// Foreach traverse 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 (1,100, 90, 80), array (2, 99, 88, 11), array (3, 10, 50, 88 )), "connect" => array (1, '000000', 'AAA @ bbb.com '), array (2, '000000', 'BBB @ ccc.com '), array (3, '20140901', '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 print_r ($ a); $ B = each ($ user); print_r ($ B ); // Array ([1] => zhangsan [value] => zhangsan [0] => name [key] => name, traverse $ 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 () with 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 echo $ arr [" key "]." => ". $ arr [" value "]."
"; // Use key and value to display key values} // use list ($ name, $ age, $ sex) = array (" zhangsan ", 10, "nnnnn"); echo $ name."
"; Echo $ age ."
"; Echo $ sex ."
"; // Another method to use list (, $ sex) = array (" zhangsan ", 10," nnnnn "); echo $ sex ."
"; // Only convert gender to variable // ip judge $ ip =" 192.168.1.128 "; list (, $ d) = explode (". ", $ ip); // explode is used. returns an array echo $ d; // retrieves 128 // list () can only receive the example of an index array $ user = array ("id" => 1, "name" => "zhangsan", "age" => 10, "sex" => "nan"); list ($ key, $ value) = each ($ user); // Array ([1] => 1 [0] => id) assigns values to parameters in the list according to the order in which the index falls, so first the 0 key and then the 1 value echo $ key. "---> ". $ value; // while list () each () use $ user = array ("id" => 1, "name" => "zhangsan ", "age" => 10, "sex" => "nan"); while (list ($ key, $ value) = each ($ user) {echo $ key. "---> ". $ value."
";}// The solution for displaying only one loop for multiple times // use the internal pointer control function of the 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); obtains the value of the current element, and the element pointed to by the index group pointer when the current element. // Key (array); obtain 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."
";}/// Move the array pointer here to the first loop below to output // reset ($ user) while (list ($ key, $ value) = each ($ user) // because each () returns false to the last one, the loop jumps out of {echo $ key. "---> ". $ value."
";} While (list ($ key, $ value) = each ($ user) // because each () returns false to the last one, so the loop jumps out of {echo $ key. "---> ". $ value."
";} Echo current ($ user)." ===> ". key ($ user);?>

The above is the basic content of php array declaration, traversal, and array global variables. For more information, see php Chinese website (www.php1.cn )!

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.