Page 1/2 of the array processing function library

Source: Internet
Author: User

Array: Create a new array.
Array_walk: Allows the User-Defined Function to process every element in the array.
Arsort: Sorts the values of an array from large to small.
Asort: Sorts the values of an array from small to large.
Count: Calculates the number of elements in a variable or array.
Current: Returns the current element of the array.
Each: Returns the index and value of the next element in the array.
End: Point the internal pointer of the array to the final element.
Key: Obtain the index data in the array.
Ksort: Sorts array elements by index.
List: Lists the values of elements in an array.
Next: Move the internal pointer of the array backward.
Pos: Returns the current element of the array.
Prev: Move the internal pointer of the array forward.
Range: Creates an array of integer ranges.
Reset: Pointer to the first element of the array.
Rsort: Sorts the values of an array from large to small.
Shuffle: Mix the order of arrays.
Sizeof: Obtain the size of the array.
Sort: Sorts arrays.
Uasort: Sorts arrays by user-defined functions.
Uksort: Sort the index of the array according to the user-defined function.
Usort: Sorts the array values by user-defined functions.

Array

Create a new array.

Syntax:Array (...);

Returned value: Array

Function Type: Data Processing

Description: The returned parameters are of the array type. The parameter can be an index with a => operator. Array () is not a regular function. It is mainly used to represent arrays.

Example: The following example shows how to create a two-dimensional array, how to specify the key value of the Union array, and how to skip and continue the Numerical index in the array.

$ Fruits = array (
"Fruits" => array ("a" => "orange", "B" => "banana", "c" => "apple "),
"Numbers" => array (1, 2, 3, 4, 5, 6 ),
"Holes" => array ("first", 5 => "second", "third ")
);

Refer to list ()

Array_walk
Allows the User-Defined Function to process every element in the array.

Syntax: int array_walk (array arr, string func );

Return Value: integer

Function Type: Data Processing

This function makes arr of each array element correspond to function name func in sequence. The element is transmitted to the first parameter of the function func. If there is more than one parameter, a warning is reported each time. To process the warning information, you can add the '@' character (to @ array_walk) before this function or use the error_reporting function.

Note: the User-Defined Function func will import the array element arr in order, so any changes to the element will affect the array itself.

Example

<?
$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Function test_alter ($ item1 ){
$ Item1 = 'bogg ';
} Function test_print ($ item2 ){
Echo "$ item2 <br> n ";
} Array_walk ($ fruits, 'test _ print ');
Array_walk ($ fruits, 'test _ alter ');
Array_walk ($ fruits, 'test _ print ');
?>
Refer to each () list ()

Arsort
Sorts the values of an array from large to small.
Syntax: void arsort (array );
Return Value: None
Function Type: Data Processing content description this function sorts the array values from large to small. The index of the array also changes with the order of values. You can use this function when you need to re-sort the order of array values in the program.

Example
The result returned by the following example is
Fruits [a] = orange
Fruits [d] = lemon
Fruits [B] = banana
Fruits [c] = apple.
We can see that the fruit name (array value) has been re-ordered by English letters from z to a, and the index also changes with the value.
<?
$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Arsort ($ fruits );
For (reset ($ fruits); $ key = key ($ fruits); next ($ fruits )){
Echo "fruits [$ key] =". $ fruits [$ key]. "n ";
}
?>

Refer to asort () rsort () ksort () sort ()

Asort
Sorts the values of an array from small to large.
Syntax: void asort (array );
Return Value: None
Function Type: Data Processing

This function sorts the values of the array from small to large. The index of the array also changes with the order of values. You can use this function when you need to re-sort the order of array values in the program.

Example
The result returned by the following example is
Fruits [c] = apple
Fruits [B] = banana
Fruits [d] = lemon
Fruits [a] = orange
We can see that the fruit name (array value) is in English
Text letters are sorted from a to z, and the index also changes with the value.
<?
$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Asort ($ fruits );
For (reset ($ fruits); $ key = key ($ fruits); next ($ fruits )){
Echo "fruits [$ key] =". $ fruits [$ key]. "n ";
}
?>

See arsort () rsort () ksort () sort ()

Count
Calculates the number of elements in a variable or array.
Syntax: int count (mixed var );
Return Value: integer
Function Type: Data Processing

This function is used to calculate the number of elements in the array (you can also substitute the variable, but the returned integer is 1 ). When the variable is not configured, the return value is 0. If the variable is not an array, the return value is 1.

Refer to sizeof () isset () is_array ()

Current
Returns the current element of the array.
Syntax: mixed current (array );
Returned value: hybrid data
Function Type: Data Processing

Description: Each array variable has an internal pointer pointing to every element of it. In addition, for interactive reference, the array has a bidirectional link table with all elements. The internal pointer of the array refers to the previously inserted element until the program executes the function with the array pointer changed. The current () function simply returns the internal pointer to the array currently indicated in the array element. It does not change the pointer value. If the array Pointer Points out of the internal pointer table, false is returned.

Note: If the array contains null elements (0 or "" Null String), this function returns false. If the current element is a null element or exceeds the array pointer, the result is of course an undefined false value. In this case, you can use the each () function.

Refer to end () next () prev () reset ()

Each
Returns the index and value of the next element in the array.
Syntax: array each (array );
Returned value: Array
Function Type: Data Processing

The returned array is the index/value pair of the current array pointer. The returned array has four elements in order: 0, 1, index, and value. The preceding 0 and index are the index of the array, and the 1 and value are the values of the array element.
Example
Example 1:
<?
$ Foo = array ("bob", "fred", "jussi", "jouni ");
$ Bar = each ($ foo );
?>
In the preceding example, the index/value of the array $ bar is returned.
0 => 0
1 => 'bob'
Key => 0
Value => 'bob'

Example 2:
<?
$ Foo = array ("Robert" => "Bob", "Seppo" => "Sepi ");
$ Bar = each ($ foo );
?>
In this example, the index/value of the array $ bar is returned.
0 => 'Robert'
1 => 'bob'
Key => 'Robert'
Value => 'bob'

Example 3:
The most typical example of an each () function is to use it with the list () function. The $ HTTP_POST_VARS variable in the following example is used.
<?
Echo "the value sent by POST is: <br> ";
While (list ($ key, $ val) = each ($ HTTP_POST_VARS )){
Echo "$ key => $ val <br> ";
}
?>

Refer to current () key () list () next () prev () reset ()

End
Point the internal pointer of the array to the final element.
Syntax: end (array );
Return Value: None
Function Type: Data Processing

This function changes the internal pointer of the array and points the pointer to the last element.

Refer to current () each () next () reset ()

Key
Obtain the index data in the array.
Syntax: mixed key (array );
Returned value: hybrid data
Function Type: Data Processing
Description
This function returns its index from the pointer to the current array.

Refer to current () next ()

Ksort
Sorts array elements by index.
Syntax: void ksort (array );
Return Value: None
Function Type: Data Processing
Description
This function sorts the elements in the array by index, and the sorted index and value still correspond

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.