array_map函数如何传递多个参数

2017-2-25 liyangweb.com 李杨 PHP

此博文仅仅讨论array_map传递多个参数的写法。

array_map有很多妙用,在我之前的一篇博文里面已经有了比较详细的介绍,详情见此:http://www.liyangweb.com/php/220.html

在我们对一个数组中的每个元素需要进行一个处理的时候,array_map是一个绝佳的选择,比如下面的这种情况:

我们需要一个函数,此函数的目的是传递一个数组(value为数字),并将所有value乘以2来返回。

/**
 * 将指定数组中的所有值乘以2后返回
 * @param array $array 格式: array(1,2,3,4,5);
 * @return array
 */
function array_multiplication($array) {
    return array_map(function($row) {
        return $row*2;
    }, $array);
}
$array = array(1,2,3,4,5);

$array_new = array_multiplication($array);
echo '<pre>';
print_r($array_new);
/*
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
    [3] => 8
    [4] => 10
)
 */

我们完成了需求,但是先不要高兴的太早,这个需求将来很可能会变成“我们需要一个函数,此函数的目的是传递一个数组(value为数字),并将所有value乘以指定倍数来返回。

这时候我们要怎么做呢?最简单的方法,将数组循环,并乘以相应的倍数。太傻了有木有,我想说的当然不是这个方法。

上面array_map中传递了一个匿名函数(也叫闭包函数),闭包本质上就是一个作用域的问题,函数内部与外部的作用域互不可见,但是我们又需要把需求中提到的指定倍数传递进去,这时候有一个use关键字(语言结构)就可以帮我们的忙了。

/**
 * 将指定数组中的所有值乘以指定倍数后返回
 * @param array $array 格式: array(1,2,3,4,5);
 * @param int $multiple
 * @return array
 */
function array_multiplication($array, $multiple) {
    return array_map(function($row) use ($multiple) {
        return $row * $multiple;
    }, $array);
}
$array = array(1,2,3,4,5);
$multiple = 3;
$array_new = array_multiplication($array, $multiple);
echo '<pre>';
print_r($array_new);
/*
Array
(
    [0] => 3
    [1] => 6
    [2] => 9
    [3] => 12
    [4] => 15
)
 */

大功告成!

标签: array_map

评论:

dvmengineers
2019-01-18 02:18
<a href="http://www.culturalsq.com/air-jordan-ultra-fly-silver-mint-green-skob">air jordan ultra fly silver mint green</a> <a href="http://www.tiaracara.com/camouflage-boston-red-sox-fitted-hat-guidelines-hata">camouflage boston red sox fitted hat guidelines</a> <a href="http://www.shaunasbags.com/billig-nike-kaishi-herre-s%C3%B8lv-runninge">billig nike kaishi herre s酶lv</a> <a href="http://www.tomtaylorblog.com/jordan-11-legend-azul-pre-order-champs-schuhea">jordan 11 legend azul pre order champs</a> <a href="http://www.pistonari.com/flyknit-roshe-run-negro-and-gris-scarpeb">flyknit roshe run negro and gris</a> <a href="http://www.rebeccastaste.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a>
dvmengineers http://www.dvmengineers.com/
fotoversorger
2019-01-16 05:06
<a href="http://www.potenzolasli.com/kobe-9-basketballe-sko-uk-obuva">kobe 9 basketballe sko uk</a> <a href="http://www.roeschtires.com/herre-ralph-lauren-london-olympic-sort-gul-polot">herre ralph lauren london olympic sort gul</a> <a href="http://www.royphelps.com/nike-roshe-run-azul-hombres-trainers-schuhed">nike roshe run azul hombres trainers</a> <a href="http://www.sekaiblog.com/nike-air-max-95-dam%C3%A4nner-himmelblau-grau-scarped">nike air max 95 dam盲nner himmelblau grau</a> <a href="http://www.agneaublanc.com/heat-3-dwyane-wade-stitched-white-nba-jersey-nflx">heat 3 dwyane wade stitched white nba jersey</a> <a href="http://www.dawndesantis.com/nike-youth-ladainian-tomlinson-limited-olivecamo-jersey-los-angeles-chargers-nfl-21-2017-salute-to-service-nflz">nike youth ladainian tomlinson limited olivecamo jersey los angeles chargers nfl 21 2017 salute to service</a>
fotoversorger http://www.fotoversorger.com/
freemoviesaz
2019-01-07 05:48
<a href="http://www.reprebates.com/air-jordan-1-retro-wings-for-venta-georgia-butya">air jordan 1 retro wings for venta georgia</a> <a href="http://www.royalwaxqueen.com/jordan-4-vivid-rosado-for-venta-obuvc">jordan 4 vivid rosado for venta</a> <a href="http://www.cliffsidesec.com/nike-air-max-95-rosa-and-nero-scarpea">nike air max 95 rosa and nero</a> <a href="http://www.deriebeeck.com/nike-roshe-run-ni%C3%B1os-p%C3%BArpura-rosado-scarpee">nike roshe run ni帽os p煤rpura rosado</a> <a href="http://www.faithstarters.com/adidas-prossoator-junior-fingersave-gloves-obuvc">adidas prossoator junior fingersave gloves</a> <a href="http://www.fotoversorger.com/nike-free-tr-unisex-grigio-scarpea">nike free tr unisex grigio</a>
freemoviesaz http://www.freemoviesaz.com/
moobletravels
2019-01-06 18:50
<a href="http://www.thedirtytaco.com/air-jordan-17-low-sort-chrome-mirror-butyc">air jordan 17 low sort chrome mirror</a> <a href="http://www.versalazer.com/nike-air-max-1-sort-cem%C3%A6ndt-laser-obuvg">nike air max 1 sort cem忙ndt laser</a> <a href="http://www.aksumat.com/kvinners-air-jordan-retro-2-bl%C3%A5-lilla-kurpesa">kvinners air jordan retro 2 bl氓 lilla</a> <a href="http://www.cletalover.com/nike-free-run-5.0-gris-femmes-tennis-kurpesf">nike free run 5.0 gris femmes tennis</a> <a href="http://www.minruteguide.com/mennns-ralph-lauren-flag-polo-beige-gr%C3%A5-polou">mennns ralph lauren flag polo beige gr氓</a> <a href="http://www.mollenteam.com/nike-roshe-run-blomstret-b%C3%B8rn-brun-sort-scarpeb">nike roshe run blomstret b酶rn brun sort</a>
moobletravels http://www.moobletravels.com/
mypotatocrop
2019-01-05 00:17
<a href="http://www.bontontravel.net/reebok-ventilator-vintage-dg-sort-schuhef">reebok ventilator vintage dg sort</a> <a href="http://www.bowdenagency.net/adidas-pr%C3%B8dator-mania-2014-pro-direct-soccer-scarpee">adidas pr酶dator mania 2014 pro direct soccer</a> <a href="http://www.braceshops.net/nike-flyknit-air-max-australia-for-verkauf-kurpesa">nike flyknit air max australia for verkauf</a> <a href="http://www.briteideaenergy.net/kvinders-nike-air-max-90-vt-lilla-orange-kurpesc">kvinders nike air max 90 vt lilla orange</a> <a href="http://www.brotherfriend.net/kobe-6-varsity-blau-one-obuva">kobe 6 varsity blau one</a> <a href="http://www.brtec.net/dam%C3%A4nner-nike-roshe-run-schwarz-anth-sail-schuhef">dam盲nner nike roshe run schwarz anth sail</a>
mypotatocrop http://www.mypotatocrop.net/
sotainvalidi
2019-01-04 17:10
<a href="http://www.blockboymusic.com/billig-nike-free-3.0-v2-herre-gr%C3%A5-sneakere">billig nike free 3.0 v2 herre gr氓</a> <a href="http://www.aquaboutic.com/jeremiah-sirles-78-jersey-xl-nfld">jeremiah sirles 78 jersey xl</a> <a href="http://www.plusjulesmumm.com/air-max-reflective-adidas-shoesc">air max reflective adidas</a> <a href="http://www.tomscave.com/air-jordan-future-red-michael-jordan-skof">air jordan future red michael jordan</a> <a href="http://www.casagranderox.com/air-jordan-retro-14-wolf-grey-sport-blue-van-sportse">air jordan retro 14 wolf grey sport blue van</a> <a href="http://www.clumsford.com/nike-free-3.0-v4-black-cheetah-runningd">nike free 3.0 v4 black cheetah</a>
sotainvalidi http://www.sotainvalidi.com/
betatodvd
2019-01-02 20:18
<a href="http://www.completefight.com/nike-tiempo-legend-blanco-que-kurpesd">nike tiempo legend blanco que</a> <a href="http://www.dealingsnet.com/air-jordan-5-fire-rouge-low-years-obuva">air jordan 5 fire rouge low years</a> <a href="http://www.eyehearsea.com/nike-kobe-11-femmes-noir-rose-schuhef">nike kobe 11 femmes noir rose</a> <a href="http://www.flixflight.com/unisexe-nike-roshe-run-floral-blanc-gris-chaussuresg">unisexe nike roshe run floral blanc gris</a> <a href="http://www.adappi.com/adidas-neo-suede-lila-gr%C3%BCn-obuvd">adidas neo suede lila gr眉n</a> <a href="http://www.berealto.com/nike-zoom-pegasus-33-dam%C3%A4nner-gelb-wei%C3%9F-kurpesc">nike zoom pegasus 33 dam盲nner gelb wei脽</a>
betatodvd http://www.betatodvd.com/
hartsigsupply
2018-12-29 23:35
<a href="http://www.schiffsamplea.com/adidas-yeezy-boost-350-v2-br%C3%B8d-yet-scarped">adidas yeezy boost 350 v2 br酶d yet</a> <a href="http://www.trouverhotel.com/air-zoom-structure-18-femmes-butya">air zoom structure 18 femmes</a> <a href="http://www.tsylospark.com/air-jordan-5-retro-rosado-and-negro-jordans-chaussuresf">air jordan 5 retro rosado and negro jordans</a> <a href="http://www.webbgiganten.com/nike-roshe-flyknit-dark-obsidian-racer-blau-wei%C3%9F-butya">nike roshe flyknit dark obsidian racer blau wei脽</a> <a href="http://www.weisseditions.com/nike-kd-9-university-rosso-fox-schuheg">nike kd 9 university rosso fox</a> <a href="http://www.zemservise.com/ralph-lauren-city-polo-navy-beige-polou">ralph lauren city polo navy beige</a>
hartsigsupply http://www.hartsigsupply.com/
ghyez348
2018-12-29 20:05
<a href="http://www.tvjuti65.com/adidas-gazelle-boost-argento-arancia-kurpese">adidas gazelle boost argento arancia</a> <a href="http://www.pbyz412s.com/air-jordan-1-blau-rot-chaussuresa">air jordan 1 blau rot</a> <a href="http://www.x745mfcu.com/nike-kyrie-3-weave-blau-rot-kurpesg">nike kyrie 3 weave blau rot</a> <a href="http://www.dkouevy6.com/nike-free-5.0-v4-rot-schuhee">nike free 5.0 v4 rot</a> <a href="http://www.15wij42f.com/jordan-hydro-5-fire-rot-obuvg">jordan hydro 5 fire rot</a> <a href="http://www.5k71d4ly.com/asics-gel-noosa-tri-10-rosa-bl%C3%A5-kurpesf">asics gel noosa tri 10 rosa bl氓</a>
[url=http://www.ghyez348.com/]ghyez348[/url]
rimsanmakine
2018-12-29 06:56
<a href="http://www.acrosstheory.com/elite-max-unger-jersey-new-orleans-saints-60-drift-fashion-gold-nflc">elite max unger jersey new orleans saints 60 drift fashion gold</a> <a href="http://www.vcclassify.com/adidas-zx-700-10.5-skoa">adidas zx 700 10.5</a> <a href="http://www.amrservicio.com/bon-march%C3%A9-nike-lebron-ambassador-9-femmes-vert-runningd">bon march茅 nike lebron ambassador 9 femmes vert</a> <a href="http://www.versalazer.com/air-jordan-jumpman-team-red-yellow-runninge">air jordan jumpman team red yellow</a> <a href="http://www.cletalover.com/toronto-blue-jays-flat-hats-name-hata">toronto blue jays flat hats name</a> <a href="http://www.nutraxtractor.com/nike-kd-9-kvinners-gull-trainersg">nike kd 9 kvinners gull</a>
rimsanmakine http://www.rimsanmakine.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1