PHP Learning Notes Array Traversal implementation code

Source: Internet
Author: User
Tags foreach array arrays execution explode variables reset
Copy CodeThe code is as follows:
<?php
/* Traversal of arrays
*
* 1. Looping through an array using a For statement
* 1. Other languages (only this one 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, array and associative array, these two cannot traverse)
*
* 2. Looping through an array using a foreach statement
* Foreacho (array variable as variable value) {
*//Circulation body
* }
* 1. The number of cycles is determined by the number of elements in the array
* 2. Each time the loop assigns the elements in the array to the following variables
*
* foreach (array variable as subscript variable => value variable) {
* }
*
*
* 3.while () list () each () combines loop traversal array
*
* each () function:
* 1. Need an array as a parameter
* 2. The return is also an array
* 3. The returned array is 0,1,key,value four subscript (fixed)
* 0 and key subscript are keys to the elements of the current parameter array
* 1 and value subscript are the values of the current parameter array elements
* 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 assigns each parameter in the list () function, and the list () converts each argument to a variable
* 4.list () can only accept indexed arrays
* 5. Assigning values to parameters by index order of subscript
*
*
*
*/
For statement Traversal array
$user =array (1, "Zhangsan", "Nan");
For ($i =0 $i <4; $i + +)
{
echo \ $user [{$i}]=]. $user [$i]. " <br> ";
}
Using foreach
$user =array (1, "Zhangsan", "Nan");
foreach ($user as $val)//$val are custom variables
{
echo $val. " <br> "//output is independent of subscript
}
foreach ($user as $key => $val)//$val $key are custom variables
{
echo $key. " =====> ". $val." <br> ";
}
foreach traverses multidimensional arrays
$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, ', ', ' aaa@bbb.com '),
Array (2, ' bbb@ccc.com ', '),
Array (3, ' 119 ', ' ccc@ddd.com ')
)
);
foreach ($info as $tableName => $table)
{
Echo ' <table align= "center" width= "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) 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) iterate backward through one of each execution
$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) the value returned when there is no element
Each () with the while traversal
$user =array ("id" =>1, "name" => "Zhangsan", "Age" =>10, "Sex" => "Nan");
while ($arr =each ($user))
{
echo $arr [0]. " ====> ". $arr [1]." <br> "//pass 0 to show key (subscript) and value
echo $arr ["Key"]. " ===> ". $arr [" Value "]." <br>//By Key,value to display the key value
}

Use of the list () function
List ($name, $age, $sex) =array ("Zhangsan", "nnnnn");
echo $name. " <br> ";
echo $age. " <br> ";
echo $sex. " <br> ";
Another way to use
List (,, $sex) =array ("Zhangsan", "nnnnn");
echo $sex. " <br> "//Only convert sex to variable
IP judgment
$ip = "192.168.1.128";
List (,,, $d) =explode (".", $ip);//explode. To separate and return an array of
echo $d//Remove 128
List () can only receive examples of 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 in the order of the index subscript, so first 0 keys and then 1 values
echo $key. " ---> ". $value;
The while list () is combined with the
$user =array ("id" =>1, "name" => "Zhangsan", "Age" =>10, "Sex" => "Nan");
while (list ($key, $value) =each ($user))
{
echo $key. " ---> ". $value." <br> ";
}

Multiple loops show only one solution at a time
Using an array's internal pointer control function
Next (array); The array pointer moves to the next
Prev (array); array pointer moves to 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 present element, the element to which the exponential group pointer points when the current element is present.
Key (array); Get the 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." <br> ";
}
Here, move the array pointer to the first loop below to output
Reset ($user)
while ($key, $value) =each ($user))//Because each () returns false to the last, the loop jumps directly
{
echo $key. " ---> ". $value." <br> ";
}
while ($key, $value) =each ($user))//Because each () returns false to the last, the loop jumps directly
{
echo $key. " ---> ". $value." <br> ";
}
Echo current ($user). =====> ". Key ($user);

?>


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.