PHP array loop operation details with instance code

Source: Internet
Author: User

PHP arrays are still commonly used. So I studied PHP array loop operations and shared them here, hoping to be useful to you. PHP is basically an array language. There are often a lot of PHP array loop operations, mainly in two ways, one is foreach, the other is while, which is good or bad is always controversial, although I have been aware of this problem for a long time, I have not studied it in detail. I have been ignorant of it until now. To save some CPU time in the future, I will summarize it as follows:

When the array "read" operation is performed in the loop, the foreach is faster than while, And the PHP array loop operation has no format to view and print the code to the clipboard?
Copy codeThe Code is as follows:
Foreach ($ arrayas $ value ){
Echo $ value;
}
While (list ($ key) = each ($ array )){
Echo $ array [$ key];
}
Foreach ($ arrayas $ value ){
Echo $ value;
}
While (list ($ key) = each ($ array )){
Echo $ array [$ key];
}

When the array "write" operation is performed in a loop, while is faster than foreach:

No format to view printed code copied to the clipboard?
Copy codeThe Code is as follows:
Foreach ($ arrayas $ key => $ value ){
Echo $ array [$ key] = $ value .'...';
}
While (list ($ key) = each ($ array )){
$ Array [$ key] = $ array [$ key]. '...';
}
Foreach ($ arrayas $ key => $ value ){
Echo $ array [$ key] = $ value .'...';
}
While (list ($ key) = each ($ array )){
$ Array [$ key] = $ array [$ key]. '...';
}

Conclusion: foreach is usually considered to be slower than while when it involves value replication, but in fact, if it is just a read operation on the array in a loop, foreach is very fast, this is because the replication mechanism adopted by PHP is "reference replication, write copy". In this way, it is not difficult to understand the efficient read operations of foreach. In addition, since foreach is not suitable for Array write operations, we can draw a conclusion that, in most cases, it is similar to foreach ($ arrayas $ key => $ value) all code in the form should be replaced with while (list ($ key) = each ($ array )).

The speed differences produced by these techniques may not be obvious in small projects, but in large projects like frameworks, a single request may involve hundreds of thousands of array loop operations, the difference is obviously magnified.

Small examples of php arrays and loops, including two-dimensional arrays, the Yang Hui triangle, the obtained parameters, and the sum of rectangular diagonal lines. If you need them, we suggest you look at them.

Copy codeThe Code is as follows:
<? Php
// 1. Use a circular statement to output any two-dimensional array.
$ Arr = array (
Array (1, 2, 3, 4 ),
Array (5, 6, 7, 8 ),
Array (9, 10, 11, 12 ),
Array (13,14, 15,16)
);
Foreach ($ arr as $ var ){
Foreach ($ var as $ val1 ){
Echo "$ val1 ";
}
Echo "<br> ";
}
Echo "<br> ";
// 2. Use the loop control statement to output the Yang Hui triangle.
Function yanghuisanjiao ($ line ){
$ SC [] [] = array ();
$ SC [0] [0] = 1;
For ($ I = 1; $ I <= $ line; $ I ++ ){
For ($ j = 0; $ j <= $ I; $ j ++ ){
If ($ j = 0 or $ I = $ j ){
$ SC [$ I] [$ j] = 1; // set the first and last numbers in each row to 1.
} Else {
$ SC [$ I] [$ j] = $ SC [$ I-1] [$ J-1] + $ SC [$ I-1] [$ j];
}
}
}
Foreach ($ SC as $ value ){
Foreach ($ value as $ v1 ){
Echo $ v1 .'';
}
Echo '<p> ';
}
}
Yanghuisanjiao (5 );
Echo "<br> ";
// 3. Obtain multiple parameters using loops and predefined variables. The number of parameters is not fixed.
Function avg (){
$ Ags = func_get_args ();
$ Sum = 0;
Foreach ($ ags as $ v ){
$ Sum + = $ v;
}
Return 'average value: '. $ sum/func_num_args ();
}
Echo avg (1, 2, 3, 4, 5, 6, 7 );

// 4. Use a loop to output a two-dimensional array and calculate the sum of the diagonal elements of the rectangle.
Function getSum ($ theCount ){
$ B = 0;
Echo '</p> ';
Echo "<table> ";
For ($ I = 1; $ I <= $ theCount; $ I ++ ){
Echo "<tr> ";
For ($ j = 1; $ j <= $ theCount; $ j ++ ){
If ($ j ==$ I | $ theCount + 1-$ I ==$ j ){
Echo "<td style = 'color: # f00'> $ j </td> ";
$ B = $ B + $ j;
If ($ j ==$ I & $ theCount + 1-$ I ==$ j ){
$ B = $ B + $ j;
}
}
Else {
Echo "<td> $ j </td> ";
}
}
Echo "</tr> ";
}
Echo "<table> ";
Echo "sum of diagonal elements:". $ B;
}
GetSum (6 );
?>

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.