PHP array learning sorting full contact page 1/2 _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP array learning sorting full access to page 12th. Array_values ($ arr) array returns all elements of the array. the code for copying all elements is as follows :? Php $ arrarray (a, B, c); $ arrarray_flip ($ arr); reverse the subscript of the array and the current $ arr array_values ($ arr) array
Returns all elements of the array.

The code is as follows:


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

The code is as follows:

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

The code is as follows:


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.

The code is as follows:


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

The code is as follows:


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

The code is as follows:


$ 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

The code is as follows:


$ 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

The code is as follows:


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

The code is as follows:


$ 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
?>


The http://www.bkjia.com/PHPjc/320418.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320418.htmlTechArticlearray_values ($ arr) array returns all elements of the array code as follows :? Php $ arr = array ("a", "B", "c"); $ arr = array_flip ($ arr ); // reverse the subscript of the array and the current $ arr =...

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.