Array processing function library 1th/2 page _php Basics

Source: Internet
Author: User
Tags mixed prev 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 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 in the array.
Each: Returns the index and value of the next element in the array.
End: Points an array's internal pointer to the last element.
Key: Gets the index data in the array.
Ksort: Sorts the elements of an array by index.
List: Lists the values of the elements in the array.
Next: Moves the internal pointer of an array backward.
Pos: Returns the current element of the array.
Prev: Moves the internal pointer of the array forward.
Range: Creates an array of integer ranges.
Reset: Points the array's pointer to the first element of the array.
Rsort: Sorts the values of an array from large to small.
Shuffle: Confuse the order of the arrays.
sizeof: Learn 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 the user-defined function.

array

to create a new array.

syntax :   array Array (...);

return value: array

function kind : Data processing

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

Use 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 numeric index in the array.

$fruits = Array (
"fruits" => Array ("A" => "Orange", "B" => "banana", "C" => "Apple"),
"Numbers" = > Array (1, 2, 3, 4, 5, 6),
"holes" => Array ("A", 5 => "second", "third")
);
reference list ()

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

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

Return value: Integer

Function kind: Data processing

Content describes this function so that each array element arr sequentially corresponds to the function name Func. The element is passed to the first parameter of the function func, and if the argument is more than one, there will be a warning message at a time. To process the warning message, precede the function with the ' @ ' character (which becomes @array_walk), or use the error_reporting function.

Note: The user Custom function Func really arr the array elements, so any changes to the elements affect the array itself.

Use the example

.?
$fruits = Array ("D" => "Lemon", "a" => "Orange", "B" => "banana", "C" => "Apple");
Function Test_alter ($item 1) {
$item 1 = ' bogus ';
} function Test_print ($item 2) {
echo ' $item 2<br>n ';
} Array_walk ($fruits, ' test_print ');
Array_walk ($fruits, ' test_alter ');
Array_walk ($fruits, ' test_print ');
?>
Reference each () list ()

The

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, from large to small. The index of the array also changes with the  order of the values. You can use this function when you need to rearrange the  order of the array values in your program.

The result returned with example
below is
fruits[a] = orange
Fruits[d] = Lemon
Fruits[b] = Banana
Fruits[c] = apple.
We can see that the fruit name (array value) has been reordered in alphabetical order from Z to a, and the index has been changed accordingly.
;?
$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 ";
}
?>

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

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

Content description This function rearranges the values of the array, from small to large. The index of the array also changes with the  order of the values. You can use this function when you need to rearrange the  order of the array values in your program.

Usage examples
The example below returns the result of the
FRUITS[C] = Apple
FRUITS[B] = Banana
FRUITS[D] = Lemon
Fruits[a] = Orange
We can see the fruit name (array value) has been pressed in English
The order of the letters is sorted by A to Z, and the index 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 ";
}
?>

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 (or to substitute a variable, except that the returned integer will be 1). When a variable is not yet configured, the return value is 0. If the variable is not an array, the return value is 1.

Reference 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

Description: Each array variable has an internal pointer to each of its elements. In addition, for interactive reference, the array has a two-way linked table of all elements. The internal pointer to the array is on the previously inserted element until the program executes to a function that has a modified array pointer. The function current () simply returns the array internal pointer that is currently being 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 an empty element (0 or "" "empty string), this function returns a value of false. If the current element is a 0-valued  element or is outside the array pointer, the result is, of course, an undetermined value of false. In this case, you can use the each () function to be more appropriate.

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

The content Description returns the index/value pairs of the array as the current array pointer. The returned array has four elements, sequentially 0, 1, index, and value. The preceding 0 and index of an array, 1 and the value of the array element.
Usage examples
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" => "Sepi");
$bar = each ($foo);
?>
An example of this, returns an array $bar index/value of
0 => ' Robert '
1 => ' Bob '
Key => ' Robert '
Value => ' Bob '

Example Three:
The most typical example of each () function is to use it with the list () function, $HTTP _post_vars variable of the following example.
?
echo "POST sends a value of:<br>";
while (the list ($key, $val) = each ($HTTP _post_vars)) {
echo "$key => $val <br>";
}
?>

Refer to current () key () list () Next () prev () Reset ( )

End
Points an array's internal pointer to the last element.
Syntax: End (array array);
return value: None
Function type: Data processing

Content description This function changes the internal pointer of the array, pointing the pointer to the last element.

Reference current () () () 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 its index from a pointer to the current array

Reference current () next ()

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

current 1/2 page   1 2 Next read the full text

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.