Php learning notes array traversal implementation code _ PHP Tutorial

Source: Internet
Author: User
Php learning notes array traversal implementation code. Copy the code as follows :? 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. quantity The code is as follows:


/* 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 );

?>

The http://www.bkjia.com/PHPjc/323575.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323575.htmlTechArticle code is as follows :? 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. number...

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.