PHP Calendar Array problem (while Iteration list () and each () calendar array principle)

Source: Internet
Author: User

Learning the array today, I met a headache for a long time, of course, for people like me just learning may have more or less help.

First, I define a two-dimensional array, as follows:

$product=Array(
Array(' Code '=' TIRE ',' Description '=' Tires ',' Price '=100 ),
array ( ' Code ' => ' oil ' , ' Description ' => ' oil ' , ' price ' =>),
array ( ' Code ' => ' SPARK ' , ' Description ' => ' Spark < Span style= "color: #008000;" >< Span style= "color: #008000;" > plugs ' < Span style= "color: #008000;" >< Span style= "color: #008000;" >, ' Price ' =>200)
);
//I personally prefer to define an array in this way, which is convenient for retrieving it.
/span> Then I used an iteration history and then added a for loop to output it (please don't ask why not forache, I haven't learned forache when it comes to this problem). As follows:
For($row=0; $row<3;  $row+ +) {
while (list($key,$value) =each($product[$row ])){
echo"| $value";
}
echo < Span style= "color: #0000ff;" >< Span style= "color: #008000;" >}
/span> < Span style= "color: #0000ff;" >< Span style= "color: #008000;" > /span> < Span style= "color: #0000ff;" >< Span style= "color: #008000;" > | tire| TIRES|100
| oil| OIL|10
| spark| Spark plugs|200

However, I have just started to play this code when there is a small problem, I accidentally the while inside of each leak, the magic result occurred, my server is not functioning properly, has been in a connected state, I am very headache, At first I thought it was a server problem,
finally found out, is indeed a server problem, because the server collapsed, after my large number of queries, I finally figured out the reason. The original principle is to use list () and each () to the array of the calendar and output. Let's take a look at this. Two function usage:
/span>
PHP each () function

The each () function generates an array of the key names and key values of the elements pointed to by the current internal pointer to the array, and moves the internal pointer forward.

The returned array contains four elements: The key is named 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data.

If the inner pointer crosses the array range, this function returns FALSE.

PHP list () function

The list () function assigns values to a set of variables using the elements in the array.

Note that, like array (), list () is actually a language structure, not a function.

It is not difficult to see that, through the list () and each () combination can output the array, when the each () function to access the address outside the array is returned false, stop the iteration, when the leakage of each, the judgment statement becomes a true value, also led to a dead loop, So that the server crashes.




PHP Calendar Array problem (while Iteration list () and each () calendar array principle)

Related Article

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.