Array processing function library 1th 2 page _php tutorial

Source: Internet
Author: User
Tags sorts
Array: Creates a new array.
Array_walk: Let the user customize the function to handle each element in the array.
Arsort: Sorts the values of the array from large to small.
Asort: Sorts the values of the array from small to large.
Count: Calculates the number of elements in a variable or array.
Current: Returns the current element in the array.
Each: Returns the index and value of the next element in the array.
End: Refers to the inner pointer of the array to the last element.
Key: Gets the index data in the array.
Ksort: Sorts the elements of the array according to the index.
List: Lists the values of the elements in the array.
Next: Moves the inner pointer of the array backwards.
Pos: Returns the current element of the array.
Prev: Moves the inner pointer of the array forward.
Range: Creates an array of integer ranges.
Reset: Refers to the array's pointer to the first element of the array.
Rsort: Sorts the values of the array from large to small.
Shuffle: Confuse the order of the arrays.
sizeof: Learns the size of the array.
Sort: Sorts the array.
Uasort: Sorts the array according to the user-defined function.
Uksort: Sorts the index of an array according to a user-defined function.
Usort: Sorts the values of the array according to a user-defined function.

array

to create a new array.

Syntax : array Array (...);

return value: array

Function type : Data processing

content description: The parameter returned is an array type. The parameter can be an index with a = operator. Array () is not actually a formal function, it is mainly used to represent arrays.

Use Example: The following example shows how to establish a two-dimensional array, how to specify the key values of the Union array, and how to skip and continue the numeric index in the array.

$fruits = Array (
"fruits" = Array ("A" and "Orange", "b" = "banana", "c" = "apple"),
"Numbers" = > Array (1, 2, 3, 4, 5, 6),
"holes" = = Array ("First", 5 = "Second", "third")
);

reference list ()

array_walk
allows the user to customize the function to handle each element in the array.

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

Return value: Integer

Function type: Data processing

Content Description This function causes each array element arr to correspond to the function name func sequentially. The element is passed to the first parameter of the Func function, and if more than one argument is passed, a warning message is provided each time. To handle the warning message, precede the function with the ' @ ' character (which becomes a @array_walk), or use the error_reporting function.

Note: The Consumer custom function func will actually substitute the array element arr sequentially, so any changes to the element will affect the array itself.

Use example

!--? $fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple" );
Function Test_alter ($item 1) {
$item 1 = ' bogus ';
} function Test_print ($item 2) {
echo $item 2
N ";
} Array_walk ($fruits, ' test_print ');
Array_walk ($fruits, ' test_alter ');
Array_walk ($fruits, ' test_print ');
?>
Reference each () list ()

arsort
Sorts the values of the array from large to small.
Syntax: void Arsort (array array);
return value: None
Function type: Data processing content Description This function rearranges the values of the array, arranged from large to small. The index of the array is also changed by the order of the values. You can use this function when you need to reorganize the order of the array values in your program.

uses the example
below to return a result of
fruits[a] = orange
Fruits[d] = Lemon
Fruits[b] = Banana
Fruits[c] = apple.
We can see that the fruit name (array value) has been reordered from Z to a in alphabetical order, and the index has changed with the value.
!--? $fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "C" and "Apple");
Arsort ($fruits);
for (Reset ($fruits), $key = key ($fruits), Next ($fruits)) {
echo "fruits[$key] =". $fruits [$key]. " n ";
}
?>

Reference Asort () Rsort () Ksort () sort ()

Asort
Sorts the values of the array from small to large.
Syntax: void Asort (array array);
return value: None
Function type: Data processing

This function re-sorts the values of the array, arranged from small to large. The index of the array is also changed by the order of the values. You can use this function when you need to reorganize the order of the array values in your program.

Usage examples
The bottom example returns the result as
FRUITS[C] = Apple
FRUITS[B] = Banana
FRUITS[D] = Lemon
Fruits[a] = Orange
We can see that the fruit name (array value) has been
The order of letters is ordered from A to Z, and the index follows the value change.
$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Asort ($fruits);
for (Reset ($fruits); $key = key ($fruits); next ($fruits)) {
echo "fruits[$key] =". $fruits [$key]. " n ";
}
?>

Reference 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 an array (you can also substitute a variable, except that the returned integer will be 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 in the array.
Syntax: Mixed current (array array);
return value: Mixed type data
function type: Data processing

Content Description: Each array variable has an internal pointer to each of its elements. In addition, for interactive reference, the array has a doubly-linked list of all the elements. An internal pointer to an array is referred to the previously inserted element until the program executes a function that changes the array pointer. The function current () simply returns the array interior pointer currently referred to in the array element. It does not change the value of the pointer, and returns FALSE if the array pointer is outside the internal pointer table.

Note: If the array contains empty elements (0 or "empty strings"), this function returns a value of false. If the current element is an empty element with a value of 0 or is outside the array pointer, the result is, of course, a false value. In this case, it is more appropriate to use the each () function.

Reference End () next () prev () reset ()

each
Returns the index and value of the next element in the array.
Syntax: array each (array array);
return value: Array
Function type: Data processing

Content description Returns an array of index/value pairs for the current array pointer. The returned array has four elements, sequentially 0, 1, index, and value. The aforementioned 0 index of an array, and 1 and the value are the values of the array element.
Use example
example one:
!--? $foo = Array ("Bob", "Fred", "Jussi", "Jouni");
$bar = each ($foo);
?
The above example returns an array $bar index/value of
0 = 0
1 = ' bob '
Key = 0
value = ' Bob '

example two:
!--? $foo = Array ("Robert" = "Bob", "Seppo" and "Sepi");
$bar = each ($foo);
?
This example returns an array $bar index/value of
0 = ' Robert '
1 = ' bob '
key = ' Robert '
value = ' Bob '
Br> example three:
The most typical example of the each () function is to be taken with the list () function, the following example $HTTP the _post_vars variable.
!--? echo "POST is sent with a value of:
";
while (list ($key, $val) = each ($HTTP _post_vars)) {
echo $key + = $val
";
}
?>

Reference current () key () list () Next () prev () reset ()

End
Refers to the inner pointer of the array to the last element.
Syntax: End (array array);
return value: None
Function type: Data processing

Description This function changes the inner pointer of an array, which points the pointer to the last element.

Refer to current () each () Next () Reset ()

Key
Gets the index data in the array.
Syntax: Mixed key (array array);
Return value: Mixed type data
Function type: Data processing
Content Description
This function returns a pointer to the current array, returning its index

Refer to Current () next ()

Ksort
Sorts the elements of the array according to the index.
Syntax: void Ksort (array array);
return value: None
Function type: Data processing
Content Description
This function sorts the elements in the array by index, and the sorted indexes and values still correspond

http://www.bkjia.com/PHPjc/317161.html www.bkjia.com true http://www.bkjia.com/PHPjc/317161.html techarticle array: Create a new array. Array_walk: Lets the user customize the function to handle each element in the array. Arsort: Sorts the values of the array from large to small. Asort: The value of the array by ...

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