PHP array sorting instances and functions

Source: Internet
Author: User
Tags shuffle

PHP array sorting instances and functions


The array in the PHP4 function manual is complete. I will check the latest manual tomorrow ....

Array_values ($ arr) array
Returns all elements of the array.
Copy the code-bkjia.com <? Php
$ Arr = array ("a", "B", "c ");
$ Arr = array_flip ($ arr); // reverse the subscript of the array and the current $ arr = array (0, 1, 2 );
$ Arr = array_values ($ arr); // returns all elements in the array $ arr.
Print_r ($ arr); // result: array (0, 1, 2 );
?>

 

Array_walk ($ arr, $ func, [$ data]) bool
Use a user-defined function to traverse all elements and return true/false.
Note: This function only processes the first dimension of the array.
$ Func is a function name.
By default, the first $ arr value and the second $ arr subscript are passed in.
Copy the code-bkjia.com <? Php
$ Arr = array ('A', 'B', 'C ');
Array_walk ($ arr, 'test'); // The second parameter test is the function name.
Function test (& $ val, $ key) // if the first parameter is referenced, modifying $ val is equivalent to modifying the element in $ arr.
{
$ Val = 'x _ '. $ val; // prefix it.
}
Print_r ($ arr); // output result array ('x _ a', 'x _ B ', 'x _ C ');
// Change $ arr to $ arr = array ('A', 'B', 'C', array (1, 2, 3 )); the printed result will be array ('x _ a', 'x _ B ', 'x _ C', 'x _ array ');
?>


The third parameter $ data is passed in. If a third parameter is passed in, the third parameter is passed to the third parameter in the function defined by the second parameter.
Copy the code-bkjia.com <? Php
Array_walk ($ arr, 'test', 'x _');
Function test (& $ val, $ key, $ prefix)
{
$ Val = $ prefix. $ val; // In fact, the $ prefix here is the above x _
}
Print_r ($ arr); // The output result is the same as the preceding array ('x _ a', 'x _ B ', 'x _ C ');
?>

 

Arsort ($ arr) bool
Arrange the array $ arr in reverse order and keep the relationship between subscript and value. If the sorting is successful, true is returned. Otherwise, false is returned.
This function only processes the first dimension of the array.
Copy the code-bkjia.com <? Php
$ Arr = array ('A' => 'A', 'B' => 'B', 'C' => 'C ');
Arsort ($ arr );
Print_r ($ arr); // print the result: array ('c' => 'C', 'B' => 'B ', 'A' => 'A'); if there is a number, the number will be before the character
?>


Asort ($ arr) bool
Arrange the array $ arr in the forward order, that is, a-z. The returned value is the same as the preceding one.
This function also retains the relationship between subscript and value.
Copy the code-bkjia.com <? Php
$ Arr = array ('A' => 'A', 'B' => 'B', 'C' => 'C ');
Asort ($ arr );
Print_r ($ arr); // The result is not changed. It is the original array.
$ Arr = array ('c' => 'C', 'B' => 'B', 'A' => 'A ');
Asort ($ arr );
Print_r ($ arr); // result: array ('A' => 'A', 'B' => 'B', 'C' => 'C ');
?>


Compact ($ varname,..., $ varname) array
Accept n $ varnames and use $ varname as the subscript $ varname value to create an array. $ varname can be an array.
I cannot explain it clearly. Let's look at the example.
Copy the code-bkjia.com <? Php
$ A = "variable ";
$ B = "variable B ";
$ Arr = compact ('A', 'B'); // input a B as the variable names defined above.
Print_r ($ arr); // The print result is array ('A' => 'variable A', 'B' => 'variable B ');
// You can also pass the variable name as an array
$ Vars = array ('A', 'B ');
$ Arr = compact ($ vars );
Print_r ($ arr); // The result is the same as above. In fact, this function is the opposite operation of extract.
?>


Extract ($ arr, $ type, $ prefix) int
Use the subscript of the array $ arr as the variable name and the value as the variable value.
$ Arr target array
$ Type: this is a constant that has been defined by PHP when the same subscript is used.
If EXTR_OVERWRITE is the same, it overwrites the previous variable. This is the default value.
If EXTR_SKIP is the same, the preceding variable is not overwritten.
If EXTR_PREFIX_SAME is the same, use the third parameter $ prefix before the variable name.
EXTR_PREFIX_ALL adds $ prefix to all variable names.
Note that the $ prefix parameter must be passed in only when $ type is EXTR_PREFIX_SAME or EXTR_PREFIX_ALL. Otherwise, it is useless to pass the parameter...
Example
Copy the code-bkjia.com <? Php
$ Arr = array ('A' => 'variable A', 'B' => 'variable B ');
Extract ($ arr, EXTR_OVERWRITE); // overwrite
Echo $ a; // The output result will be 'variable'
Echo $ B; // result: 'variable B'
$ Arr = array ('A' => 'variable A', 'B' => 'variable B ', 'A' => 'second variable '); // The subscript of both elements is.
Extract ($ arr );
Echo $ a; // The output result is: 'Second variable A' obviously overwrites 'variable a' because the default second parameter is EXTR_OVERWRITE.
?>

 

Count ($ arr) int
Count the number of elements in the array
Copy the code-bkjia.com <? Php
$ Arr = array ('A', 'B ');
Echo count ($ arr); // The result is obviously 2.
?>

 

Current ($ arr) mixed
Returns the element indicated by the current pointer in the array. This function alias is pos.
Copy the code-bkjia.com <? Php
$ Arr = array ('A', 'B', 'C ');
Echo current ($ arr); // The result is 'A'
Echo next ($ arr); // the pointer moves to the next one, so now the pointer refers to B. The output result is 'B' of course'
Echo current ($ arr); // The result is B because the current pointer is in B.
Echo end ($ arr) // the pointer moves to the end of the array and returns the result. Therefore, the result is c.
Echo prev ($ arr); // the pointer is moved up. Result B
Echo key ($ arr); // returns the subscript of the element indicated by the pointer. Because the pointer is directed to B, all results are 1, because the subscript of B is 1.


Echo reset ($ arr); // reset the pointer to all the results starting with array
?>

Each ($ arr) array
Returns a key/value (subscript/value) in the heavy array $ arr.
Copy the code-bkjia.com <? Php
$ Arr = array ('A' => 1, 'B' => 2, 'c' => c );
$ Res = each ($ arr );
Print_r ($ res); // The result is array ('0' => 'A', 'key' => 'A', 1 => 1, 'value' => 1)
// The returned result 0 and key both represent subscript 1 and value represent values.
// If the each array is not used once, the internal pointer will be moved down once. If it is already at the end of the array, false will be returned.
$ Res = each ($ arr );
Print_r ($ res); // The result is converted to array ('0' => 'B', 'key' => 'B', 1 => 2, 'value' => 2)
?>


List ($ val,..., [$ val]) void no return value
Assign values in the array to some variables $ val
Copy the code-bkjia.com <? Php
$ Arr = array ('color', 'Letters A', 'Letters B ');
List ($ color, $ a, $ B) = $ arr; // note that the $ val position of the list parameter corresponds to the position in the array, and the value is left to the right.
Echo $ color; // result 'color'
Echo $ a; // result 'A'
List ($ color, $ B) = $ arr; // empty
Echo $ color; // result 'color'
Echo $ a; // none of the results are the same, because a is not assigned a value.
Echo $ B; // result 'B'
// Use list each together
$ Arr = array ('color', 'Letters A', 'Letters B ');
While (list ($ key, $ val) = each ($ arr )){
Echo 'subscript: '. $ key;
Echo '---- value:'. $ val;
Echo '<br> ';
}
// The output result is
// Subscript: 0 ---- value: Color
// Subscript: 1 ---- value: Letter
// Subscript: 2 ---- value: Letter B
?>

 

Krsort ($ arr, [$ type]) bool
Sort the array $ arr in reverse order according to its subscript
There is also a ksort (); this is sorted in ascending order by subscript
$ Type is the sorting method.
Copy the code-bkjia.com <? Php
$ Arr = array ('A' => 1, 'B' => 2, 'c' => 3 );
Krsort ($ arr );
Print_r ($ arr); // print the result: array ('c' => 3, 'B' => 2, 'A' => 1 );
?>

 

Range ($ go, $ end, [$ setup) array
This is not easy to explain.
Copy the code-bkjia.com <? Php
$ Arr = range (1, 10 );
Print_r ($ arr); // result array (1, 2, 3, 4,..., 10 );
$ Arr = range (1, 10, 2); // specify the third parameter, that is, the step size.
Print_r ($ arr); // result array (1, 3, 5, 7, 9 );
$ Arr = range ('A', 'z ');
Print_r ($ arr); // result array ('A', 'B', 'C',..., 'z ');
?>


Sort ($ arr );
Sort the array by a-z.
Copy the code-bkjia.com <? Php
$ Arr = array ("lemon", "orange", "banana", "apple ");
Sort ($ arr );
Print_r ($ arr); // result array ('apple', 'bana', 'limon', 'Orange ');
// There is also a z-a sort function rsort
?>


Shuffle ($ arr) bool
Sorts the array $ arr randomly.
Copy the code-bkjia.com <? Php
$ Arr = range (1, 10 );
Shuffle ($ arr );
Print_r ($ arr); // I do not know what the result is, because it is random.
?>

 

Usort ($ arr, $ func) bool
Use your own function to sort $ arr $ func is a custom function.
Copy the code-bkjia.com <? Php
$ Arr = array (1, 5, 8, 2, 0, 3 );
Usort ($ arr, 'test ');
Function test ($ a, $ B ){
Return $ a = $ B? 0: $ a <$ B? 1:-1;
}
Print_r ($ arr ); // print the result Array ([0] => 8 [1] => 5 [2] => 3 [3] => 2 [4] => 1 [5] => 0)
// I don't understand this function .. I don't know what the parameter $ a and $ B represent? I can't understand the manual either. If you use ksort for such a simple sorting, you can use rsort.
// There are two such functions, uksort, and uasort...
?>

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.