"PHP" Array usage (GO)

Source: Internet
Author: User

Summary:

Foreach,while,for the array traversal method, it is recommended to use foreach (PHP internal implementation, the simplest speed, and the ability to traverse the class properties). And some common methods Current,prev,next,end,key,reset,each

First, foreach

foreach () is the simplest and most efficient way to iterate through the data in an array.

<? PHP $colors Array (' Red ', ' blue ', ' green ', ' yellow '); foreach ($colors as $color) {    echo$color<br/> ";}? >

Show Results:

Like red?
Do you like blue?
Do you like green?
Do you like yellow?

<? PHP $colors Array (' color1 ' = ' red ', ' color2 ' = ' blue '); foreach ($colorsas$key= +$value)    {echo$key $value? <br/> ";}? >

Show Results:

Do you like Color1 red?

Color2 Blue?

Second, while

while () is usually used in conjunction with list (), each ().

<? PHP $colors Array (' Red ', ' blue ', ' green ', ' yellow ');  while (list($key,$valeach ($colors)) {    echo $val. <br/> ";}? >

Show Results:

Other list of red.
Other list of blue.
Other list of green.
Other list of yellow.

Third, for

<? PHP $arr Array ("0" = "Zero", "1" = "One", "2" and "one");  for ($i= 0; $i Count ($arr$i+ +) {  $str$arr[$i]    ; Echo $str. <br/> ";}? >

Show Results:

The number is zero.
The number is one.
The number is a.

Iv. Common methods of arrays

1) The current () array value in the position of the Target_array array. Unlike the next (), Prev (), and end () functions, current () does not move the pointer.

2) Next () returns the array value immediately following the next position in the current array pointer.

3) prev () returns the array value at the previous position of the current pointer, or False if the pointer is in the first position of the array.

4) the end () pointer moves to the last position of the Target_array and returns the last element.

<?PHP$fruits=Array("Apple", "orange", "banana");$fruit= Current($fruits);//return "Apple"Echo $fruit." <br/> ";$fruit=Next($fruits);//return "Orange"Echo $fruit." <br/> ";$fruit=prev($fruits);//return "Apple"Echo $fruit." <br/> ";$fruit=End($fruits);//return "Banana"Echo $fruit." <br/> ";?>

Show Results:

Apple
Orange
Apple
Banana

5) each () returns the current key/value pair of the input array and advances the pointer to a position. The returned array contains four keys, key 0 and key contain the key names, and key 1 and value contain the corresponding data. Returns False if each () is executed before the pointer is at the end of the array.

<? PHP $capitals Array ("Ohio" = "Columbus", "towa" = "Des Moines", "Arizona" and "Phoenix"); $s 1  each ($capitals); Print_r ($s 1);? >

Show Results:

Array ([1] = Columbus [value] = Columbus [0] = = Ohio [key] = Ohio)

6) Reset ()

Used to set the Input_array pointer back to the beginning of the array. This function is often used when you need to view or work with the same array multiple times in a script, and this function is often used when sorting ends.

<?PHP$colors=Array(' Red ', ' blue ', ' green ', ' yellow ');foreach($colors as $color) {Echo"Do-like$color? <br/> ";}Reset($colors); while(List($key,$val)= each($colors)) {Echo"$key=$val<br/> ";}?>

Show Results:

Like red?
Do you like blue?
Do you like green?
Do you like yellow?
0 = Red
1 = Blue
2 = Green
3 = Yellow

Note: Assigning an array to another array resets the original array pointer, so in the example above, if we assign the $colors to another variable inside the loop, it will cause an infinite loop.
For example will $s 1 = $colors; Add to the while loop and execute the code again, and the browser will display the results endlessly.

7) Key ()

Returns the key element in the Input_array at the current pointer position.

<?PHP$capitals=Array("Ohio" = "Columbus", "towa" = "Des Moines", "Arizona" and "Phoenix");Echo"<p>can you name the capitals of these states?</p>"; while($key=Key($capitals)) {    Echo $key." <br/> "; Next($capitals); //each key () call does not push the pointer. To do this, use the next () function}?>

Show Results:

Can you name the capitals of these states?
Ohio
Towa
Arizona

"PHP" Array usage (GO)

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.