PHP Array processing function

Source: Internet
Author: User
Current

Current--Returns the cell in the array

Description

Mixed Current(Array &array)

Each array has an internal pointer to its "current" cell, which initially points to the first cell inserted into the array.

The current () function returns the value of the array cell that is currently pointed to by the internal pointer and does not move the pointer. If the internal pointer points beyond the end of the cell list, current() returns FALSE.

Warning

if the array contains empty cells (0 or "", an empty string), the function returns False when it encounters this unit. This makes it impossible to use current () to determine whether the end of this array list is reached. to properly traverse an array that may contain empty cells, use the Each () function.

Example 1. Examples of usage of current () and related functions

$transport = array (' foot ', ' bike ', ' car ', ' plane ' );
$mode = current ($transport);
//$mode = ' foot ';
$mode = Next ($transport);
//$mode = ' bike ';
$mode = current ($transport);
//$mode = ' bike ';
$mode = prev ($transport);
//$mode = ' foot ';
$mode = End ($transport);
//$mode = ' plane ';
$mode = current ($transport);
//$mode = ' plane ';
?>

-------------------------------------------------------------------------------------------------------

End

End--Points the inner pointer of the array to the last cell

Description

Mixed End(Array &array)

end () moves the internal pointer of the array to the last cell and returns its value.

Example 1. Simple End () example

$fruits = Array (' Apple ', ' banana ', ' cranberry ' );
Echo End ($fruits);
//Cranberry
?>

-------------------------------------------------------------------------------------------------------

Prev

Prev--Rewind The internal pointer of the array back to a

Description

Mixed prev(Array &array)

Returns the value of the previous cell pointed to by the inner pointer of the array, or FALSEif there are no more cells.

Warning

if the array contains empty cells, or if the value of the cell is 0, the function encounters these cells and returns FALSE. To properly traverse an array that may contain empty cells or a cell value of 0, see the each () function.

The behavior of prev () and next () is similar except that it returns the internal pointer back to one instead of a forward position.

Example 1. Prev () and correlation function usage examples

$transport = array (' foot ', ' bike ', ' car ', ' plane ' );
$mode = current ($transport);
//$mode = ' foot ';
$mode = Next ($transport);
//$mode = ' bike ';
$mode = Next ($transport);
//$mode = ' car ';
$mode = prev ($transport);
//$mode = ' bike ';
$mode = End ($transport);
//$mode = ' plane ';
?>

-------------------------------------------------------------------------------------------------------

Next

Next-Moves the inner pointer in the array forward one

Description

Mixed Next(Array &array)

Returns the value of the next cell pointed to by the array's internal pointer, or FALSEif there are no more cells.

Next () and current () behave Similarly, with only a little difference, moving the inner pointer forward one bit before the return value. This means that it returns the value of the next array cell and moves the array pointer forward one . If the result of moving the pointer is beyond the end of the array cell, then next () returns FALSE.

Warning

If the array contains empty cells, or if the value of the cell is 0, the function encounters these cells and returns FALSE. To properly traverse an array that may contain empty cells or a cell value of 0, see the Each () function.

Example 1. Next () and usage examples of related functions

$transport = array (' foot ', ' bike ', ' car ', ' plane ' );
$mode = current ($transport);
//$mode = ' foot ';
$mode = Next ($transport);
//$mode = ' bike ';
$mode = Next ($transport);
//$mode = ' car ';
$mode = prev ($transport);
//$mode = ' bike ';
$mode = End ($transport);
//$mode = ' plane ';
?>

-------------------------------------------------------------------------------------------------------

Key

Key--Get the key name from the associative array

Description

Mixed Key(Array &array)

Key () returns the key name of the current cell in the array.

Example 1. Key () example

!--? php
$array
= Array (
' fruit1 ' =& Gt ' Apple '
,
' fruit2 ' = ' orange '
,
' fruit3 ' = ' grape '
,
' fruit4 ' + ' apple '
,
' fruit5 ' = ' apple '
);

//This cycle echoes all associative array
//key where value equals ' Apple '
while ($FR Uit_name = current ($array
)) {
if ($fruit _name = = ' Apple '
) {
echo key ($array). '
'
;
}
Next ($array
);
}
?

-------------------------------------------------------------------------------------------------------

Reset

Reset--point the inner pointer of the array to the first cell

Description

Mixed Reset(Array &array)

Reset () returns the internal pointer of the array back to the first cell and returns the value of the first array cell, FALSEIf the array is empty.

Example 1. Reset () example

"!--? php

$array = Array (' Step one ', ' step ', ' s Tep three ', ' step Four '
);

//By default, the pointer are on the first element
Echo current ($array). "
/n";
//"Step One"

//Skip Steps
Next ($array
);
Next ($array
); br> Echo Current ($array). "
/n";
//"Step three"

//reset pointer, start again on step one
Reset ($array
);
Echo cu Rrent ($array). "
/n";
//"Step one"

?

-------------------------------------------------------------------------------------------------------

each

Each--Returns the current key/value pair in the array and moves the array pointer one step forward

Description

Array each(Array &array)

Returns the key/value pair of the current pointer position in an array and moves the array pointer forward. A key-value pair is returned as an array of four cells with the key name 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 internal pointer crosses the end of the array, each () returns FALSE.

Example 1. Each () example

$foo = Array ("Bob", "Fred", "Jussi", "Jouni", "Egon", "Marliese" );
$bar = each ($foo
);
Print_r ($bar
);
?>

$bar now contains the following key/value pairs:

Array {[1] = bob [value] = Bob [0] = 0 [key] = 0}

$foo = Array ("Robert" = "Bob", "Seppo" = "Sepi" );
$bar = each ($foo
);
Print_r ($bar
);
?>

$bar now contains the following key/value pairs:

Array {[1] = bob [value] = Bob [0] = Robert [Key] = Robert}

Each () is often used in conjunction with list () to iterate through an array, for example:

Example 2: Iterating through an array with each ()

$fruit = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' cranberry ' );
Reset ($fruit
);
while (list ($key, $val) = each ($fruit
)) {
echo "$key =-$val/n"
;
}
?>

The example above will output:

A = apple b = Banana c = Cranberry

After each () , the array pointer stays in the next cell in the array, or when the end of the array is encountered, the last cell. If you want to iterate through the array again , you must use Reset ().

  • 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.