Share PHP array_walk() function usage tips_PHP tutorial

WBOY
Release: 2016-07-15 13:33:09
Original
1037 people have browsed it

PHP array_walk() function definition and usage

PHP array_walk() function for each element in the array Apply callback function. Returns TRUE if successful, FALSE otherwise.

Typically function accepts two parameters. The value of the array parameter is used as the first one, and the key name is used as the second one. If the optional parameter userdata is provided, it will be passed to the callback function as the third parameter.

If function requires more arguments than given, an E_WARNING level error will be generated each time array_walk() calls function. These warnings can be suppressed by preceding the array_walk() call with PHP's error operator @, or by using error_reporting().

PHP array_walk() function syntax

array_walk(array, function, userdata...)

PHP array_walk() function parameters with description

array required. Specifies an array.
function required. The name of the user-defined function.
userdata optional. The value entered by the user can be used as a parameter of the callback function.

PHP array_walk() function tips and comments

Tip: You can set one or more parameters for the function.

Note: If the callback function needs to act directly on the values ​​in the array, you can specify the first parameter of the callback function as a reference: &$value. (See Example 3)

Note: Passing key names and userdata into functions is new in PHP 4.0.

PHP array_walk() function example 1

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>function myfunction($value,$key)   </span></li><li class="alt"><span>{  </span></li><li><span>echo "The key $key has the value<br /> $value</span><span class="tag"><</span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>";  </span></span></li>
<li class="alt"><span>}  </span></li>
<li>
<span>$</span><span class="attribute">a</span><span>=</span><span class="attribute-value">array</span><span>("a"=</span><span class="tag">></span><span>"Cat","b"=</span><span class="tag">></span><span>"Dog",<br>"c"=</span><span class="tag">></span><span>"Horse");  </span>
</li>
<li class="alt"><span>array_walk($a,"myfunction");  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
<li class="alt"><span> </span></li>
</ol>
Copy after login

Output:

The key a has the value Cat
The key b has the value Dog
The key c has the value Horse


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446073.htmlTechArticlePHP array_walk() function definition and usage The PHP array_walk() function applies a callback function to each element in the array. Returns TRUE if successful, FALSE otherwise. Typically funct...
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