Home  >  Article  >  Backend Development  >  PHP array function array_walk usage and examples

PHP array function array_walk usage and examples

墨辰丷
墨辰丷Original
2018-06-02 10:01:011450browse

This article mainly introduces the usage of PHP array function array_walk, and analyzes the usage skills of array_walk to call user functions for each member of the array. Friends who need it can refer to it

The details are as follows:

$words=array("l"=>"lemon","o"=>"orange","b"=>"banana","a"=>"apple");
//定义一个回调函数,输出数组元素
function words_print($value,$key,$prefix){
  echo "$prefix:$key=>$value
\n"; } //定义一个回调函数直接改变元素的值 function words_alter(&$value,$key){ $value=ucfirst($value); $key=strtoupper(key); } //输出元素的值 array_walk($words,'words_print','words'); //改变元素的值 array_walk($words,'words_alter'); echo "
";
print_r($words);
echo "
";

Example of class internal calls:

class ArrayWalk {
  /**
  * properties:
  */
  var $body_chunk = array('0'=>'Dewen', '1'=>'PHP', 2=>'Linux');
  /////////////////////////////////////////////////
  // VARIABLE METHODS
  /////////////////////////////////////////////////
  function ArrayWalk (){
  }
  function func_1(){
  print_r($this->body_chunk);
  array_walk ($this->body_chunk, array($this,'SpellStrToLower'));
  print_r($this->body_chunk);
  }
  function SpellStrToLower (&$str){
    $str = strtolower ($str);
  }
}
$obj = new ArrayWalk();
echo '
';
$obj->func_1();
echo '
';

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpDetailed explanation of WeChat development access example

PHP MySQL implementation fuzzy Query employee information function

PHP MySQL implements the function of jumping to the specified page by entering the page number

The above is the detailed content of PHP array function array_walk usage and examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn