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
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools