PHP Learning Notes Array Traversal implementation Code _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/* 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/323575.html www.bkjia.com true http://www.bkjia.com/PHPjc/323575.html techarticle Copy the code as follows:? PHP/* Array Traversal * * 1. Use the For statement to iterate through the array * 1. Other languages (only this way) * 2.PHP in this way is not the preferred way * 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.