PHP:数组操作函数array_walk()和array_map()
array_map()的函数原型为:array array_map ( callback callback, array arr1 [, array...] )
array_map() 返回一个数组,该数组包含了arr1中的所有单元经过callback作用过之后的单元。callback接受的参数数目应该和传递给 array_map() 函数的数组数目一致。
callback函数就是array_map所将调用来处理元素单元函数,应以字符串的形式将函数名传递给array_map()
如:(php官方手册所给出的代码)
function cube($n)
{
return($n * $n *$n);
}
$a = array(1,2, 3, 4, 5);
$b= array_map("cube",$a);
print_r($b);
?>
cube即为callback函数 ,且array_map的callback函数的参数每一个应均为数组的元素,也就是array_map()除了第一个参数外,其余参数应均为数组且应与回调函数的参数个数一样.
<?php //callback 1 function check($n) { //array_mapcallback的参数为数组的元素, //也就是callback中有几个参数array_map就应传入几个数组 if($n>100) { return $n-10; }else { return $n; } } //callback 2 function add($a,$b) { return $a+$b; } $arr = array(101,85,35,105,99,109);// var_dump($arr); $brr = array(1,2,3,4,5,6); $arr = array_map("check", $arr); $brr = array_map("add", $arr,$brr); print_r($arr); echo "<br/>"; print_r($brr); ?>
输出结果
array (size=6) 0 => int 101 1 => int 85 2 => int 35 3 => int 105 4 => int 99 5 => int 109
Array ( [0] => 91 [1] => 85 [2] => 35 [3] => 95 [4] => 99 [5] => 99 )
Array ( [0] => 92 [1] => 87 [2] => 38 [3] => 99 [4] => 104 [5] => 105 )
========================================================================================
我是分割线
========================================================================================
与array_map()不同array_walk()的返回值是布尔型,也就是如果想要修改数组的数据,应该在callback函数上做手脚(也就是引用)
array_walk()会将数组的元素的值,以及键值传递给callback函数,此外还允许传递其类型的数据给callback函数.
以下为其原型以及官方文档:
bool array_walk ( array &array, callback funcname [,mixed userdata] )
如果成功则返回 TRUE,失败则返回 FALSE。
将用户自定义函数funcname应用到array数组中的每个单元。典型情况下funcname接受两个参数。array参数的值作为第一个,键名作为第二个。如果提供了可选参数userdata,将被作为第三个参数传递给 callbackfuncname。
如果funcname函数需要的参数比给出的多,则每次 array_walk() 调用funcname时都会产生一个E_WARNING 级的错误。这些警告可以通过在 array_walk() 调用前加上 PHP 的错误操作符 @ 来抑制,或者用 error_reporting()。
注: 如果funcname需要直接作用于数组中的值,则给funcname的第一个参数指定为引用。这样任何对这些单元的改变也将会改变原始数组本身。
注: 将键名和 userdata 传递到funcname中是 PHP 4.0新增加的。
array_walk() 不会受到array内部数组指针的影响。array_walk() 会遍历整个数组而不管指针的位置。
用户不应在回调函数中改变该数组本身。例如增加/删除单元,unset 单元等等。如果 array_walk() 作用的数组改变了,则此函数的的行为未经定义,且不可预期。
以下是我自己写的一个简单的例子:
<?php function alter(&$value,$key,$userdata) { //在此处不对元素值做任何修改,如果想要修改则需像我一样给参数加上引用 echo $key.$userdata.$value.'<br/>'; } $arr = array("a"=>"A","b"=>"B","c"=>"C","d"=>"D"); //array_walk -- 对数组中的每个成员应用用户函数 //函数原型:bool array_walk ( array &array, callback funcname [, mixed userdata] ) //第一个参数为 数组 //第二个参数为 callback函数名, //第三个参数为 可选参数(将传递给所callback的函数) //============================================================================== //array_map()将传递callback 函数的 的 //第一个参数是元素的值,如需修改则应加上 &操作符 //第二个参数为键值,如需修改则作如上处理 //第三个参数为用户自己传递给函数的数据(即array_walk()中的第三个参数) array_walk($arr,'alter',".is."); ?>
输出
a.is.A
b.is.B
c.is.C
d.is.D
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
