Dynamic Web technology PHP's array processing function library

Source: Internet
Author: User
Tags array arrays empty html form shuffle sort sorts mysql database

Array: Creates a new array.
Array_walk: Lets a user customize a function to handle every element in an 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 present element in the array.
Each: Returns the index and value of the next element in the array.
End: The inner pointer of an array is pointed to the last element.
Key: Gets the index data in the array.
Ksort: Sorts the elements of an array according to the index.
List: Lists the values of the elements in the array.
Next: Moves the internal pointer of the array backward.
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: 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

Creates a new array.

Syntax: Array array (...);

return value: Array

Function type: Data processing

Content Description: The returned parameter 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 type: Data processing

Content description This function makes each array element arr sequentially relative 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 element, so any changes to the element will affect the array itself.

Usage examples

?
$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 ');
?>
Refer to each () list ()

Arsort
Sorts the values of an 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.

Usage examples
The example below returns the result of the
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 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

Usage examples
?
$fruits = Array ("D" => "Lemon", "a" => "Orange", "B" => "banana", "C" => "Apple");
Ksort ($fruits);
for (Reset ($fruits); $key = key ($fruits); next ($fruits)) {
echo "fruits[$key] =". $fruits [$key]. " \ n ";
}
?>
In the example above, the returned data is
Fruits[a] = Orange
FRUITS[B] = Banana
FRUITS[C] = Apple
FRUITS[D] = Lemon

Reference Arsort () Asort () sort () Rsort ()

List
Lists the values of the elements in the array.
Syntax: void list (...);
return value: None
Function type: Data processing

The content description and the array function are not normal functions. The list () function is used to list the values in all arrays.

Usage examples
The following example lists the array data returned from the MySQL database.
<table>
<tr>
<th> Employee Name </th>
<th> Salary </th>
</tr>
<?php
$result = MySQL ($conn, "SELECT ID, Name, salary from employees");
while (list ($id, $name, $salary) = Mysql_fetch_row ($result)) {
Print ("<tr>\n").
"<td><a href=\" info.php3?id= $id \ "> $name </a></td>\n".
"<td> $salary </td>\n".
"</tr>\n");
}
?></table>

Reference each () array ()

Next
Moves the internal pointer of an array backward.
Syntax: Mixed next (array array);
Return value: Mixed type data
Function type: Data processing
Content Description This function returns an internal pointer to the next element of the array, or false value if there is no next element. Of course, if an empty array, it also returns a value of false. To properly handle an array that may be empty, you can use the each () function. This function looks like the current () function, where the function causes the pointer to point back to the next element and returns the value of the element at the new pointer, meaning that the pointer moves backward and returns the next element value.

Reference current () end () prev () Reset ()

Pos
Returns the current element of the array.
Syntax: Mixed pos (array array);
Return value: Mixed type data
Function type: Data processing
Content Description
This function is actually the current () function.
Reference
End () Next () prev () Reset ()

Array processing function library
Prev
Moves the internal pointer of the array forward.
Syntax: Mixed prev (array array);
Return value: Mixed type data
Function type: Data processing
Content Description This function returns an internal pointer to the previous element of the array, or false value if there is no previous element. Of course, if an empty array, it also returns a value of false. To properly handle an array that may be empty, you can use the each () function. This function is somewhat like the next () function, except that the prev () function moves the pointer forward, and the next () function moves the pointer backward.

Refer to current () end () Next () Reset ()

Range
Creates an array of integer ranges.
Syntax: array range (int low, int high);
return value: Array
Function type: Data processing
Content Description This function creates an array of contiguous integers from small to large. Use examples in the shuffle () function
Reference Shuffle ()

Reset
Points the array's pointer to the first element of the array.
Syntax: Mixed reset (array array);
Return value: Mixed type data
Function type: Data processing
Content Description This function resets the pointer of the array and changes the pointer to the first element of the array. The content of the returned value is also the first element of the array.
Usage examples
Tip: Kk@shonline.de points out (30-jul-1998) that when the data in the array is not stable, the use of Reset () will have a warning message. The most often occurs when processing an HTML form (form) input data, if the user input data is not sufficient, or users intentionally adding or deleting input fields, there may be a warning message. Use the @reset () method to make the warning message not appear (before reset (), but it's just a way to deceive yourself; the way to get the axe to pay is to use if (Isset ()) (see the 4.48.14 isset () function), as the following example

?
if (Isset ($form _array)) {
Reset ($form _array);
while (the list ($k, $v) = each ($form _array)) {
Do_something ($k, $v);
}
}
?>

Refer to current () each () Next () prev ()

Rsort
Sorts the values of an array from large to small.
Syntax: void Rsort (array array);
return value: None
Function type: Data processing
Content Description
This function will rearrange the array in the form of large to small.
Usage examples
?
$fruits = Array ("Lemon", "orange", "banana", "apple");
Rsort ($fruits);
for (Reset ($fruits); list ($key, $value) = each ($fruits); {
echo "fruits[$key] =". $value. \ n ";
}
?>
The result after processing is
Fruits[0] = Orange
FRUITS[1] = Lemon
FRUITS[2] = Banana
FRUITS[3] = Apple

Reference Arsort () Asort () Ksort () sort () Usort ()

Shuffle
Confuse the order of the arrays.
Syntax: void shuffle (array array);
return value: None
Function type: Data processing
Content Description This function causes the array to shuffle all elements in a random manner.
Usage examples
?
$numbers = range (1,20);
Srand (Time ());
Shuffle ($numbers);
while (the list (, $number) = each ($numbers)) {
echo "$number";
}
?>
Attention:
Ccunning@math.ohio-state.edu points out (31-may-1999) that there is a problem with this function on Solaris because of the effect of the LIBC function library. The current solution is as follows example. (Note: Perhaps the new PHP version will fix the problem later.) )
?
function Myshuffle ($array) {
Mt_srand (Double) microtime () *1000000);
$num = count ($array);
For ($i =0 $i < $num-1; $i + +) {
$n = Mt_rand ($i +1, $num);
$temp = $array [$n];
$array [$n] = $array [$i];
$array [$i] = $temp;
}
}
$array = Array (1,2,3,4,5,6,7,8,9,10);
Myshuffle (& $array);
while (list (, $var) =each ($array)) {
Echo $var. " ";
}
?>

Reference Arsort () Asort () Ksort () Rsort () sort () Usort ()

sizeof
Learn the size of the array.
Syntax: int sizeof (array array);
return value: Integer
Function type: Data processing
Content Description
This function returns the number of elements of an array as an integer type.
Reference count ()

Sort
Sorts the array.
Syntax: void sort (array array);
return value: None
Function type: Data processing
Content description This function will rearrange the array in small to large ways.
Usage examples
?
$fruits = Array ("Lemon", "orange", "banana", "apple");
Sort ($fruits);
for (Reset ($fruits); list ($key, $value) = each ($fruits); {
echo "fruits[$key] =". $value. \ n ";
}
?>
The result returned is
Fruits[0] = Apple
FRUITS[1] = Banana
FRUITS[2] = Lemon
FRUITS[3] = Orange

Reference Arsort () Asort () Ksort () Rsort () Usort ()

Uasort
Sorts the array according to the user-defined function.
Syntax: void Uasort (array array, function cmp_function);
return value: None
Function type: Data processing
Content description This function will rearrange the array in a user-determined manner, and of course the index and value of the element still maintain a corresponding relationship. When you feel you need to design your own sort, you can customize the processing function and then use this function to sort the array values.
Reference Arsort () Asort () Ksort () Rsort () sort () Uksort ()

Uksort
Sorts the index of an array according to a user-defined function.
Syntax: void Uksort (array array, function cmp_function);
return value: None
Function type: Data processing
Content description This function cmp_function the index of the array in a user-defined way, which is the user-developed function.

Usage examples
?
function Mycompare ($a, $b) {
if ($a = = $b) return 0;
return ($a > $b)? -1:1;
}
$a = Array (4 => "Four", 3 => "three", => "twenty", => "ten");
Uksort ($a, mycompare);
while (the list ($key, $value) = each ($a)) {
echo "$key: $value \ n";
}
?>
The above program returns the data as
20:twenty
10:ten
4:four
3:three
Reference Arsort () Asort () Ksort () Rsort () sort () Uasort ()

Usort
Sorts the values of the array according to the user-defined function.
Syntax: void Usort (array array, function cmp_function);
return value: None
Function type: Data processing
Content Description
This function resets the value of the array to the user-defined way. You can use this function if you need to use a special sort method in your program.

Usage examples
?
function cmp ($a, $b) {
if ($a = = $b) return 0;
return ($a > $b)? -1:1;
}
$a = array (3,2,5,6,1);
Usort ($a, CMP);
while (the list ($key, $value) = each ($a)) {
echo "$key: $value \ n";
}
?>
The return value of this example is
0:6
1:5
2:3
3:2
4:1
Reference Arsort () Asort () Ksort () Rsort () sort ()



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.