Dynamic web skills PHP array processing function library

Source: Internet
Author: User
Array: creates a new array. Array_walk: allows a user-defined function to process every element in the array. Arsort: sorts the array values from large to small. Asort: sorts the array values from small to large. Count: calculates the elements in a variable or array.

Array: creates a new array.
Array_walk: allows a user-defined function to process every element in the array.
Arsort: sorts the array values from large to small.
Asort: sorts the array values 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 the internal pointer of the array to the final element.
Key: obtain the index material 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 categories.
Reset: pointer of the array to the first element of the array.
Rsort: sorts the array values from large to small.
Shuffle: mix the order of arrays.
Sizeof: obtains the size of the array.
Sort: sorts arrays.
Uasort: sorts arrays by user-defined functions.
Uksort: sorts array indexes by user-defined functions.
Usort: sorts the array values by user-defined functions.

Array

Create a new array.

Syntax: array (...);

Returned value: Array

Function type: Material 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 important to express arrays.

Application 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' => 'bana', 'C' => 'apple '),
'Numbers '=> array (1, 2, 3, 4, 5, 6 ),
'Calles' => 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: Material Processing

Clarify this function so that each array element arr corresponds to the 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 apply the error_reporting function.

Note: The app's custom function func will actually represent the array element arr in order, so any transformation to the element will affect the array itself.

Application model

$ Fruits = array ('D' => 'limon', 'a' => 'Orange ',' B '=> 'bana ', 'C' => 'apple ');
Function test_alter ($ item1 ){
$ Item1 = 'bogg ';
} Function test_print ($ item2 ){
Echo '$ item2
\ 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: material processing content clarification this function sorts array values from large to small. The index of the array also follows the value? Changes in order. When you need to refresh the array values in the program? This function can be applied in order.

Application model
The results returned by the following examples are:
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' => 'limon', 'a' => 'Orange ',' B '=> 'bana ', '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: Material Processing

Content clarification this function sorts the values of the array from small to large. The index of the array also follows the value? Changes in order. When you need to refresh the array values in the program? This function can be applied in order.

Application model
The results returned by the following examples are:
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' => 'limon', 'a' => 'Orange ',' B '=> 'bana ', 'C' => 'apple ');
Asort ($ fruits );
For (reset ($ fruits); $ key = key ($ fruits); next ($ fruits )){
Echo 'Fruits [$ key] = '. $ fruits [$ key].' \ n ';
}
?>
 
See arsort () rsort () ksort () sort ()

Count
Calculate the number of elements in a variable or array.
Syntax: int count (mixed var );
Return value: integer
Function type: Material Processing

Content clarification this function is used to calculate the number of elements in the array (variable can also be substituted, but 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 of the array.
Syntax: mixed current (array );
Returned value: hybrid material
Function type: Material Processing

Clarification: 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 element originally inserted, until the program executes the function with the corrected array pointer. The current () function 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 zero value? Null element or an array pointer is exceeded. The result is of course an undefined false value. In this case, you can apply 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: Material 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.
Application model
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', 'secpo' => 'secpi ');
$ 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 the each () function is to use it with the list () function. The $ http_post_vars variable in the following example is used.
Echo 'post sends the following values:
';
While (list ($ key, $ val) = each ($ http_post_vars )){
Echo '$ key => $ val
';
}
?>

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: Material Processing

Content clarification this function will change the internal pointer of the array, which points the pointer to the last element.

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

Key
Obtain the index material in the array.
Syntax: mixed key (array );
Returned value: hybrid material
Function type: Material Processing
Content clarification
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: Material Processing
Content clarification
This function sorts the elements in the array by index, and the sorted index and value still correspond

Application model
$ Fruits = array ('D' => 'limon', 'a' => 'Orange ',' B '=> 'bana ', 'C' => 'apple ');
Ksort ($ fruits );
For (reset ($ fruits); $ key = key ($ fruits); next ($ fruits )){
Echo 'Fruits [$ key] = '. $ fruits [$ key].' \ n ';
}
?>
In the example above, the returned materials are
Fruits [a] = orange
Fruits [B] = banana
Fruits [c] = apple
Fruits [d] = lemon
 
See arsort () asort () sort () rsort ()

List
Lists the values of elements in an array.
Syntax: void list (...);
Return value: None
Function type: Material Processing

Like array functions, it is not a formal function. The list () function is used to list the values in all arrays.

Application model
The following example lists the array materials returned from the mysql database.














$ Result = mysql ($ conn, 'Select id, name, salary from ployees ');While (list ($ id, $ name, $ salary) = mysql_fetch_row ($ result )){Print (' \ N '.' \ N '.' \ N '.' \ N ');}?>
Employee Name Salary
$ Name$ Salary


See each () array ()

Next
Move the internal pointer of the array backward.
Syntax: mixed next (array );
Returned value: hybrid material
Function type: Material Processing
Content clarification this function returns the internal pointer of the next element in the array. if there is no next element, false is returned. Of course, if it is an empty array, it also returns false. To accurately process arrays that may be empty, you can apply the each () function. This function looks like the current () function. The difference is that this function enables the pointer to forward to the next element and returns the element value at the New pointer, that is, the pointer moves backward, and return the value of the next element.

Refer to current () end () prev () reset ()

Pos
Returns the current element of the array.
Syntax: mixed pos (array );
Returned value: hybrid material
Function type: Material Processing
Content clarification
This function is actually the current () function.
Reference
End () next () prev () reset ()

Array Processing function library
Prev
Move the internal pointer of the array forward.
Syntax: mixed prev (array );
Returned value: hybrid material
Function type: Material Processing
Content clarification this function returns the internal pointer of the first element of the array. if there is no previous element, false is returned. Of course, if it is an empty array, it also returns false. To accurately process arrays that may be empty, you can apply the each () function. This function is a bit like the next () function, but the prev () function moves the pointer forward, while the next () function moves the pointer backward.

Refer to current () end () next () reset ()

Range
Creates an array of integer categories.
Syntax: array range (int low, int high );
Returned value: Array
Function type: Material Processing
This function creates a continuous integer array from small to large. There are application models in shuffle () functions
Refer to shuffle ()

Reset
Pointer to the first element of the array.
Syntax: mixed reset (array );
Returned value: hybrid material
Function type: Material Processing
Content clarification this function resets the pointer of the array and changes the pointer to the first element of the array. The returned value is also the first element of the array.
Application model
Tip: kk@shonline.de pointed out (30-jul-1998), when the array of material is not solid enough, the application reset () will have a warning. Most often, when processing the input material of an html form (form), if the input material is not sufficient, or the user intentionally adds or deletes the input field, there may be warning information. The @ reset () method can be used to prevent the warning information from being displayed (add @ before reset (). However, this is only a method to hide your ears and ears; the method to apply the if (isset () method (see the 4.48.14 isset () function) is as follows:

If (isset ($ form_array )){
Reset ($ form_array );
While (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 );
Return value: None
Function type: Material Processing
Content clarification
This function sorts the array in a descending order.
Application model
$ Fruits = array ('limon', 'Orange ', 'bana', '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


Refer to arsort () asort () ksort () sort () usort ()

Shuffle
Mix the order of arrays.
Syntax: void shuffle (array );
Return value: None
Function type: Material Processing
Content clarification this function randomly sorts all elements in an array.
Application model
$ Numbers = range (1, 20 );
Srand (time ());
Shuffle ($ numbers );
While (list (, $ number) = each ($ numbers )){
Echo '$ number ';
}
?>
Note:
The ccunning@math.ohio-state.edu pointed out (31-may-1999) that, on solaris, the function is rendered with a title due to the impact of the libc function library. The current solution is as follows. (Note: This title may be changed later in the new php version .)
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 );
Myshuffle (& $ array );
While (list (, $ var) = each ($ array )){
Echo $ var .'';
}
?>

See arsort () asort () ksort () rsort () sort () usort ()

Sizeof
Obtain the size of the array.
Syntax: int sizeof (array );
Return value: integer
Function type: Material Processing
Content clarification
This function returns the number of elements in the array as an integer.
Refer to count ()

Sort
Sorts arrays.
Syntax: void sort (array );
Return value: None
Function type: Material Processing
Content clarification this function will rearrange the array in a way from small to large.
Application model
$ Fruits = array ('limon', 'Orange ', 'bana', 'apple ');
Sort ($ fruits );
For (reset ($ fruits); list ($ key, $ value) = each ($ fruits );){
Echo 'Fruits [$ key] = '. $ value.' \ n ';
}
?>
The returned result is
Fruits [0] = apple
Fruits [1] = banana
Fruits [2] = lemon
Fruits [3] = orange
 
Refer to arsort () asort () ksort () rsort () usort ()

Uasort
Sorts arrays by user-defined functions.
Syntax: void uasort (array, function cmp_function );
Return value: None
Function type: Material Processing
Content clarification this function rearranges the array according to the user-defined method. of course, the index and value of the element still maintain the corresponding relationship. When you feel that you need to design your own sorting method, you can first customize the processing function, and then apply this function to sort the array values.
See arsort () asort () ksort () rsort () sort () uksort ()

Uksort
Sort the index of the array according to the user-defined function.
Syntax: void uksort (array, function cmp_function );
Return value: None
Function type: Material Processing



Content clarification this function rearranges the index of the array according to the user-defined method. cmp_function is the function developed by the user.


Application model
Function mycompare ($ a, $ B ){
If ($ a = $ B) return 0;
Return ($ a> $ B )? -1: 1;
}
$ A = array (4 => 'Four ', 3 => 'Three', 20 => 'twenty', 10 => 'Ten ');
Uksort ($ a, mycompare );
While (list ($ key, $ value) = each ($ )){
Echo '$ key: $ value \ n ';
}
?>
The Returned Materials of the above program are
20: twenty
10: ten
4: four
3: three
See arsort () asort () ksort () rsort () sort () uasort ()

Usort
Sorts the array values by user-defined functions.
Syntax: void usort (array, function cmp_function );
Return value: None
Function type: Material Processing
Content clarification
This function sorts the array values according to the user-defined method. If a special sorting method is required in the program, you can apply this function.

Application model
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 (list ($ key, $ value) = each ($ )){
Echo '$ key: $ value \ n ';
}
?>
In this example, the return value is
0: 6
1: 5
2: 3
3: 2
4: 1
See 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.