Home > Backend Development > PHP Tutorial > Usage examples of php function array_walk

Usage examples of php function array_walk

*文
Release: 2023-03-18 19:52:01
Original
1415 people have browsed it

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 in need can refer to it. I hope it will be helpful to everyone.

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<br>\n";
}
//定义一个回调函数直接改变元素的值
function words_alter(&$value,$key){
  $value=ucfirst($value);
  $key=strtoupper(key);
}
//输出元素的值
array_walk($words,&#39;words_print&#39;,&#39;words&#39;);
//改变元素的值
array_walk($words,&#39;words_alter&#39;);
echo "<pre class="brush:php;toolbar:false">";
print_r($words);
echo "
";
Copy after login

Example of internal calls in class:

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

Related recommendations:

php merge array function array_merge()

Introduction to php array functions and summary of array sorting methods

php array function definition and usage summary

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template