Use of PHP array functions

Source: Internet
Author: User

1.array_walk ($arr, $func, [$data])

Use a user-defined function to traverse all elements and return True/false

$func is the name of a function
By default, the value of the first $arr of the two parameters is passed, the second $arr is subscript, and a pair of the two passes OH

1<?PHP2 $arr=Array(' A ', ' B ', ' C ');3 Array_walk($arr, ' Test ');//here the second parameter, test, is the name of a function .4 functionTest (&$val,$key)//here the first argument plus the reference, then modifying the $val is equivalent to modifying the elements in the $arr5 {6 $val= ' X_ '.$val;//and give him a prefix here .7 }8 Print_r($arr);//output result Array (' x_a ', ' x_b ', ' x_c ');9 //If you change the $arr to $arr = Array (' A ', ' B ', ' C ', array (1, 2, 3)), the printed result will be an array (' x_a ', ' x_b ', ' x_c ', ' X_array ');Ten?>
If there is a third parameter passed in, the third parameter is passed to the third parameter in the function defined by the second parameter
1<?PHP2 Array_walk($arr, ' Test ', ' x_ ');3 functionTest (&$val,$key,$prefix){4     $val=$prefix.$val;//actually, the $prefix here is the x_.5 }6 Print_r($arr);//The output is the same as the above array (' x_a ', ' x_b ', ' x_c ');7?>

2. How to iterate through arrays: each and List

1 //method One:2 $sports=Array(3' Football ' = ' good ',4' Swimming ' = ' very well ',5' Running ' = ' not good ');6  while($elem= each($sports)) {7     Echo $elem[' Key ']. ":".$elem[' Value ']. " <br/> ";8 }9  Ten //Method Two: One $sports=Array( A' Football ' = ' good ', -' Swimming ' = ' very well ', -' Running ' = ' not good '); the  while(List($key,$value) = each($sports)) { -     Echo $key.": ".$value." <br/> "; - } - //Football:good + //Swimming:very well - //Running:not good +  

Use of PHP array functions

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.