Phparray processing functions

Source: Internet
Author: User
Phparray handler current

Current -- returns the current unit in the array.

Description mixedCurrent(Array & array)

Each array has an internal pointer pointing to its "current" unit, initially pointing to the first unit inserted into the array.

Current ()The function returns the value of the array unit pointed to by the internal pointer and does not move the pointer. If the internal pointer points to the end of the cell list,Current ()ReturnFALSE.

Warning

If the array contains an empty unit (0 or "", null string), this function returns FALSE when this unit is encountered.This allowsCurrent ()It is impossible to determine whether it is at the end of this array list.To traverse arrays that may contain null cells correctly, useEach ()Function.

Example 1.Current ()And related function Usage examples

$ 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 ';
?>

Bytes -------------------------------------------------------------------------------------------------------

End

End -- points the internal pointer of the array to the last unit.

Description mixedEnd(Array & array)

End ()Move the internal pointer of array to the last unit and return its value.

Example 1. Simple end () example

$ Fruits = array ('apple', 'bana', 'Cranberry');
Echo end ($ fruits );
// Cranberry
?>

Bytes -------------------------------------------------------------------------------------------------------

Prev

Prev -- returns the internal pointer of the array to one

Description mixedPrev(Array & array)

Returns the value of the previous unit pointed to by the internal pointer of the array, or if no more units exist.FALSE.

Warning

If the array contains empty cells or the unit value is 0, FALSE is returned when this function encounters these cells. To correctly traverse arrays that may contain null cells or whose unit value is 0, seeEach ()Function.

Prev ()AndNext ()The behavior is similar, except that it only reverts the internal pointer back to one, instead of moving forward to one.

Example 1. prev () and related 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 ';
?>

Bytes -------------------------------------------------------------------------------------------------------

Next

Next -- move the internal pointer in the array to a forward position

Description mixedNext(Array & array)

Returns the value of the next unit pointed to by the internal pointer of the array, or if no more units exist.FALSE.

Next ()AndCurrent ()The behavior is similar. there is only one difference. before returning the value, move the internal pointer one byte forward.This means that it returns the value of the next array unit and moves the array pointer one bit forward.. If the result of moving the pointer is beyond the end of the array unitNext ()ReturnFALSE.

Warning

If the array contains empty cells or the unit value is 0, this function also returnsFALSE. To correctly traverse arrays that may contain null cells or whose unit value is 0, seeEach ()Function.

Example 1. use next () and 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 ';
?>

Bytes -------------------------------------------------------------------------------------------------------

Key

Key -- fromJoin arrayObtain the key name

Description mixedKey(Array & array)

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

Example 1. key ()

$ Array= Array (
'Fruit1' => 'apple'
,
'Fruit2' => 'Orange'
,
'Fruit3' => 'grape'
,
'Fruit4' => 'apple'
,
'Fruit5' => 'apple'
);

// This cycle echoes all associative array
// Key where value equals "apple"
While ($ fruit_name = current ($ array
)){
If ($ fruit_name = 'apple'
){
Echo key ($ array ).'
'
;
}
Next ($ array
);
}
?>

Bytes -------------------------------------------------------------------------------------------------------

Reset

Reset -- point the internal pointer of the array to the first unit

Description mixedReset(Array & array)

Reset ()Returns the internal pointer of array to the first unit and returns the value of the first array unit. If the array is emptyFALSE.

Example 1. reset ()


$ Array = array ('step one', 'step two', 'step three ', 'step four'
);

// By default, the pointer is on the first element
Echo current ($ array )."
/N ";
// "Step one"

// Skip two steps
Next ($ array
);
Next ($ array
);
Echo current ($ array )."
/N ";
// "Step three"

// Reset pointer, start again on step one
Reset ($ array
);
Echo current ($ array )."
/N ";
// "Step one"

?>

Bytes -------------------------------------------------------------------------------------------------------

Each

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

Description arrayEach(Array & array)

Returns the key/value pair of the current pointer position in the array and moves the array pointer forward. The key-value pair is returned as an array of four units, with the key name0, 1, key and value. Unit0 and key contain the key name of the array unit, and 1 and value contain data.

If the internal pointer crosses the end of the array, each () returns FALSE.

Example 1. each ()

$ 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 ()Frequent andList ()Used in combination to traverse arrays, for example:

Example 2. use each () to traverse the array

$ Fruit = array ('a' => 'apple', 'B' => 'bana', 'C' => 'crancel');
Reset ($ fruit
);
While (list ($ key, $ val) = each ($ fruit
)){
Echo "$ key => $ val/n"
;
}
?>

The above example will output:

a => apple b => banana c => cranberry

In executionEach ()Then, the array pointer will stay in the next unit of the array or stay in the last unit when it comes to the end of the array. If you wantReuseThe each must be used to traverse the array.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.