PHP array function array_walk usage and examples

墨辰丷
Release: 2023-03-29 12:08:01
Original
1569 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 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<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 class internal calls:

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!