Home  >  Article  >  Backend Development  >  PHP array function array_walk usage example, array array_walk_PHP tutorial

PHP array function array_walk usage example, array array_walk_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:46941browse

Usage example of php array function array_walk, array_walk

This article describes the usage of php array function array_walk. Share it with everyone for your reference, 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,'words_print','words');
//改变元素的值
array_walk($words,'words_alter');
echo "<pre class="brush:php;toolbar:false">";
print_r($words);
echo "
";

Example of internal class call:

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 '<PRE>';
$obj->func_1();
echo '
';

Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:

php code online formatting and beautification tool:

http://tools.jb51.net/code/phpformat

In addition, since php belongs to the C language style, the following tool can also format php code:

C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json

Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133078.htmlTechArticlephp array function array_walk usage example, array array_walk This article describes the usage of php array function array_walk. Share it with everyone for your reference, the details are as follows: $words=array("l"...
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