Wednesday, April 20, 2011

TIP: PHP array_map with multiple arguments

If you want to use array_map with a native php function that has multiple arguments, do the following.

Our task is to remove the hyphen "-" from each array and replace it with " ". Instead of using a foreach lets use
PHP's array_map():

        $hyphenated = array('fat-dog', 'small-dog', 'thin-dog');
        $container = array_map('str_replace',
                               array_fill(0, count($hyphenated), '-'),
                               array_fill(0, count($hyphenated), ' '), $hyphenated);
Result:
array('fat dog', 'small dog', 'thin dog');   

A thing to note here. The second argument and so forth of array_map reflects exactly the required arguments of str_replace(mixed $search , mixed $replace , mixed $subject) in order so for the example above str_replace $search argument goes in argument number 2 of array_map which is "array_fill(0, count($hyphenated)" (notice how i fill the array to match the subject array's count?), same for the second and the third is the $subject itself which is $hyphenated.

In addition, array_map will only accept an array type on its second and consecutive arguments and also needs to match the count number of subject array or the remaining array values will not be populated at all.

 Now if you are using PHP 5.3 it is better to use Closures or Lambda function:

array_map(function ($content) {
  $content = str_replace('-', ' ', $content);
  $content = ucwords($content);
  return $content;
}, array('fat-dog', 'small-dog', 'thin-dog'));


Now that is neat!!



40 comments:

Anonymous said...

thanks for posting that, used it for running a function on the contents of a directory

array_map('get_file_stuff', scandir('files'), array_fill(0, count(scandir('files'), 'myParameter')

co wendy said...

Sergei , October 22, 1970 Born in Christian Louboutin Bois Dore Moscow State Ozherelye. Perhaps influenced by Cheap LV Handbags their parents worship warrior army, join the army in cheap jordans 1989, and into the elite airborne military service. 1991, by the influence of the Soviet Union, the uggs on sale Russian christian louboutin army general lack of funds, a large number ugg australia of Air Jordan 11 Gamma Blue soldiers seeking veterans, but under extremely difficult conditions still dedicated, praise superiors, and soon was sent to the famous christian louboutin shoes Ryazan Higher ugg Airborne Command School studies. After graduating in 1994, entered the famous Pskov 76th Guards Airborne discount christian louboutin Division, served as discount nike jordans the reconnaissance platoon, reconnaissance ugg soldes deputy company commander, company commander, battalion airborne regiment scouting director and other duties.From 2000 to Cheap Louis Vuitton Handbags 2004, where the forces have repeatedly ordered war with Chechen militants. Because of their opponents in order to form the squad activity and haunted impermanence, the Russians had to ugg boots mobilize elite troops set up large uggs outlet number of small units, "a Discount Louis Vuitton small wholesale jordan shoes play small" Implementation siege. commanding troops in Chechnya, Christian Louboutin Daffodile Ingushetia and other places over the mountains, to track the militants fled. Discount LV Handbags Although the militants were extremely vigilant, but as long as the task has never had outsmarted.August 8, 2008, Georgia suddenly invade South Ossetia (Russian peacekeepers stationed ugg pas cher there), the Russian military to respond quickly, including including the 76th Guards Airborne Division, more than christian louboutin remise 50% 3,000 people were immediately delivered to the pro-Russian Abkhazia, Georgia cheap nike jordan shoes Army contain two infantry brigades, effectively coordinate the cheap christian louboutin direction of the Russian Bags Louis Vuitton troops in South Ossetia.

Guo Guo said...

yao20150324
michael kors handbags
thomas sabo
valentino shoes
ralph lauren outlet
mac cosmetics
true religion outlet
tods outlet
adidas wings
iphone 6 cases
thomas sabo uk
hollister clothing
ray ban uk
ralph lauren outlet
abercrombie
cheap oakley sunglasses
mont blanc
hollister shirts
burberry outlet online
burberry handbags
replica watches
converse shoes
ghd
tods outlet
toms shoes
lacoste shirts
michael kors outlet online
michael kors handbags
louis vuitton outlet
ferragamo shoes
soccer jerseys
michael kors outlet
ray ban sunglasses

Unknown said...

new york giants jerseys
juicy couture outlet
chanel outlet
ray ban sunglasses
robert griffin jersey,joe theismann jersey,andre roberts jersey,sonny jurgensen jersey,art monk jersey,bashaud breeland jersey,barry cofield jersey,perry riley jersey,e.j. biggers jersey,duke ihenacho jersey,josh morgan jersey
mizuno shoes
seahawks jersey
stuart weitzman sale
nike free,nike free run,free running,free run,nike running
five fingers shoes
tory burch outlet online
uggs outlet
cheap oakley sunglasses
cheap jordans
mizuno running shoes
derrick rose jersey
cowboys jerseys
barcelona soccer jersey
bottega veneta wallet
nike roshe
chanel outlet,chanel handbags,chanel bags,chanel outlet,chanel 2.55
supra shoes
giants jersey
stephen curry jersey
miami dolphins jerseys
bottega veneta outlet
indianapolis colts jerseys
nike air max 2015
ray ban outlet
arizona cardinals jerseys
the north face clearance
coach outlet store
nike trainers
chelsea jersey
washington redskins jerseys
zhuweiming0627

chenlina said...

chenlina20150711
ray ban sunglasses
imitation watches
louis vuitton handbags
abercrombie store
chanel bags
chanel handbags
air max 90
soccer cleats
louis vuitton
coach outlet
michael kors uk
concords 11
chanel outlet
oakley sunglasses
gucci handbags
true religion sale
hollister clothing
cheap chanel handbags
insanity dvd
lebron 11
michael kors uk
louboutin
fendi handbags
louis vuitton handbags
louis vuitton
coach outlet store online
longchamp outlet
toms shoes
hollister
ray ban glasses
cheap jerseys wholesale
mulberry handbags
chanel outlet
louis vuitton outlet
coach outlet online
fitflop sale
michael kors outlet
ray ban outlet
louboutin
michael kors handbags

Hua Cai said...

the north face clearance
pandora jewelry
salomon running shoes
cheap ray ban sunglasses
tory burch outlet online
new york giants jerseys
toms outlet
cheap soccer jerseys
adidas outlet
lacoste polo shirts
pandora jewelry
chicago bears jerseys
cheap oakley sunglasses
air max 90
lacoste outlet
hollister
ray ban sunglasses
mulberry outlet
michael kors outlet online
stuart weitzman boots
oakley sunglasses
miami heat jersey
links of london
kate spade outlet
air force one shoes
burberry handbags
koby bryant shoes
manchester united jersey
air max 2015
denver broncos jerseys
polo ralph lauren outlet
christian louboutin outlet
the north face outlet
hollister shirts
valentino shoes
hollister clothing
chanel outlet
mcm bags
nike roshe
true religion jeans
2015717caihuali

Gege Dai said...

15.07.20fangyanting
soccer jerseys for sale
abercrombie & fitch
kate spade handbags
air jordan uk
longchamp outlet online
pandora charms
rolex watches for sale
coach outlet store online
coach outlet
true religion jeans outlet
pandora bracelets
oakley sunglasses wholesale
ralph lauren polo shirts
kate spade bags
coach outlet
snapbacks hats
toms shoes for women
soccer shoes for sale
ray bans
beats headphones
hollister kids
gucci handbags
cheap oakley sunglasses
replica watches for sale
coach factory outlet
gucci borse
gucci belt
cheap snabacks
hollister outlet
tory burch outlet
kate spade bags
michael kors
air max 2015
longchamp pliage pas cher
mcm outlet online

Zheng junxai5 said...

2015-09-16 zhengjx
Ralph Lauren UK Sale Polo Shirts
Christian Louboutin Outlet For Women
ugg boots
Louis Vuitton Outlet Authentic Handbags Discount
michael kors handbags
Louis Vuitton Handbags Outlet On Sale
Louis Vuitton Outlet Purses and Handbags For Sale
canada goose
hermes birkin
New Jordans Cheap For Sale
Abercrombie & Fitch Clothing With Big Discount
Coach Factory Outlet Handbags 70% OFF
Lebron 12,11,10 Shoes For Sale
cheap uggs sale
Michael Kors Outlet Online Sale Free Shipping
Louis Vuitton Handbags Outlet Sale
Air Jordan 4 Retro Free Shipping
coach outlet store online
Louis Vuitton Handbags Outlet Stores Online
Ray Ban Sunglasses Outlet Store Online
ugg boots
michael kors handbag
michael kors outlet
Abercrombie And Fitch New York Clothing Warehouse
ralph lauren uk
Cheap Oakley Sunglasses Outlet With Discount
tory burch outlet
nike air max
Coach Outlet Store Online Free Shipping
Michael Kors Outlet USA Store

Wenhao Guo said...

cheng0928
louis vuitton handbags, http://www.louisvuittonhandbag.us/
hermes birkin, http://www.hermesbirkin.org/
mbt shoes, http://www.mbtshoesoutlet.us.com/
swarovski outlet, http://www.swarovski-outlet.co.uk/
hogan, http://www.scarpehogan-outlet.it/
ray ban, http://www.occhiali-rayban.it/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
nba jerseys, http://www.nbajerseys.us.com/
longchamp handbags, http://www.longchamphandbag.us.com/
cheap nhl jerseys, http://www.nhljerseys.us.com/
cheap mlb jerseys, http://www.cheapmlbjerseys.net/
north face jackets, http://www.thenorthfaces.in.net/
cheap snapbacks, http://www.cheapsnapbacks.us.com/
adidas wings, http://www.adidaswings.net/
basketball shoes, http://www.basketballshoes.us.com/
lebron james shoes, http://www.lebronjamesshoes.in.net/
lacoste polo shirts, http://www.lacostepoloshirts.cc/
vans shoes, http://www.vans-shoes.cc/
ugg boots, http://www.uggboot.com.co/
hermes belt, http://www.hermesbelts.us/

mao qiuyun said...

ray ban sunglasses, http://www.raybansunglassesonline.us.com/
nike trainers, http://www.niketrainers.me.uk/
gucci handbags, http://www.guccihandbags-outlet.co.uk/
herve leger, http://www.herveleger.us.com/
timberland boots, http://www.timberlandboots.name/
coach outlet online, http://www.coachoutletonline-store.us.com/
michael kors outlet, http://www.michaelkorsoutletcanada.in.net/
juicy couture outlet, http://www.juicycoutureoutlet.net/
calvin klein underwear, http://www.calvinklein.in.net/
ray ban sunglasses, http://www.raybansunglass.us.com/
prada outlet, http://www.pradaoutlet.us/
lululemon outlet, http://www.lululemonoutletstore.in.net/
tory burch shoes, http://www.toryburchshoesoutlet.com/
michael kors outlet, http://www.michaelkorsoutlet.org.uk/
replica watches, http://www.replicawatchesforsale.us.com/
pandora jewelry, http://www.pandorajewelryoutlet.us.com/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
michael kors outlet, http://www.michaelkorsoutletonlinstore.us.com/
oakley outlet, http://www.oakleyoutlet.in.net/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
michael kors bags, http://www.michaelkorsbags.uk/
ray ban sunglasses, http://www.ray-bansunglasses.eu.com/
oakley sunglasses, http://www.oakley.in.net/
air max 2015, http://www.airmax2015.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
swarovski outlet, http://www.swarovski-outlet.co.uk/
pandora jewelry, http://www.pandora.eu.com/
chanel handbags, http://www.chanelhandbags-outlet.co.uk/
1002maoqiuyun

chenlina said...

chenlina20151127
replica watches
ugg sale
lebron shoes
coach outlet store online
uggs boots
canada goose jackets
ugg outlet
louis vuitton handbags
gucci shoes
ray ban outlet store
mont blanc legend
michael kors handbags
ray ban sunglasses
louis vuitton handbags
cheap uggs
hollister co
kobe 9
adidas superstars
hollister
north face jackets
tory burch sale
ugg australia
louis vuitton handbags
timberland outlet
michael kors outlet online
canada goose jackets
louis vuitton outlet
hollister co,hollister jeans,hollister.com,hollister ca
coach outlet store online
michael kors outlet
louis vuitton purses
michael kors outlet
uggs on sale
adidas originals
louis vuitton
louis vuitton handbags
michael kors outlet online
giuseppe zanotti
nike roshe run women
ugg boots on sale
as

حسام داود said...

companies in Riyadh cleaning workers technical coach at the highest levelشركة تسليك مجارى بالرياض
شركة تنظيف بالرياض
شركة تنظيف شقق بالرياض
شركة تنظيف منازل بالرياض
شركة تنظيف خزنات بالرياض
شركة مكافحة حشرات بالرياض
شركة رش مبيدات بالرياض
شركة تخزين اثاث بالرياض
شركة تنظيف مجالس بالرياض
شركة تنظيف فلل بالرياض

方松腾 said...

bottega veneta outlet online
burberry outlet
cheap toms
polo ralph lauren outlet
coach outlet store online,coach outlet store,coach outlet
nba jerseys wholesale
moncler outlet
ugg boots clearance
valentino shoes
michael kors factory sale
rolex watches
kobe bryants shoes 2015
mont blanc
true religion jeans,cheap true religion jeans,true religion jeans wholesale,true religion outlet,true religion sale,true religion outlet online
ray-ban sunglasses
ysl outlet online
louis vuitton outlet
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
hermes outlet online
cheap oakley sunglasses
celine outlet
cleveland cavaliers
football shirts uk,soccer jerseys uk,cheap soccer jerseys uk
ferragamo shoes
pandora charms
ralph lauren,ralph lauren uk,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,ralph lauren polo
omega watches
michael kors uk outlet
burberry outlet
christian louboutin

chenlina said...

chenlina20160127
michael kors outlet
burberry outlet
chanel handbags
ralph lauren outlet
cheap jordans
beats solo
ugg boots sale
louis vuitton outlet
true religion outlet
gucci handbags
cheap oakley sunglasses
louboutin
air max 95
chanel outlet
mihchael kors outlet online
jordan 11 concord
louis vuitton handbags
gucci outlet
kate spade
ray ban sunglasses
toms shoes
uggs outlet
ray ban sunglasses
nike air max
michael kors handbags
coach outlet store online
oakley sunglasses
coach factory outlet
louis vuitton purses
canada goose jackets
louis vuitton
replica watches
michael kors outlet
louis vuitton outlet stores
ray ban outlet store
marc jacobs handbags
louis vuitton
louboutin pas cher
tiffany and co
cheap oakley sunglasses
as

jualobatkuatcialisasli45. said...

Obat Ejakulasi Dini
Obat Kuat Alami
Obat Lemah Syahwat
Obat Disfungsi Ereksi
Obat Kuat Pria Perkasa
Obat Kuat Nangen Zengzhangsu
Obat Kuat Tahan Lama
Perangsang Ereksi Pria
Obat Kuat Viagra
Obat Kuat Levitra
Obat Kuat Procomil Spray
Vimax Asli Canada
Cobra Oil Super
minyak Lintah Papua
Boneka Full Body Asli

Yuanyuan Lin said...

6.29llllllyuancheap nfl jersey
chrome hearts outlet
true religion outlet
true religion jeans
tory burch outlet
nike air huarache
nike huarache
cartier sunglasses
michael kors outlet
ferragamo shoes
coach outlet online
christian louboutin outlet
beats headphones
louis vuitton handbags
louis vuitton pas cher
true religion outlet
tiffany outlet
ralph lauren pas cher
herve leger outlet
christian louboutin shoes
christian louboutin uk
timberland shoes
michael kors canada
michael kors factory store
nba jerseys
tiffany outlet
basketball shoes
toms shoes
air jordan shoes for sale
adidas wings
mulberry handbags
ralph lauren outlet
ferragamo shoes
michael kors handbags
ralph lauren polo
6.29

John said...

basketball shoes
nhl jerseys wholesale
coach outlet store online
pandora jewelry outlet
yeezy boost 350
ralph lauren uk
cheap nfl jerseys
kobe bryant shoes
adidas gazelle
rolex watches uk
gucci handbags outlet
wholesale nike shoes
michael kors outlet clearance
cheap ray bans
adidas nmd
ray ban sunglasses
nike cortez white
ed hardy uk
supra shoes
coach outlet store online
north face outlet
red bottoms outlet online
ghd flat iron
skechers shoes
timberland shoes
michael kors outlet online
abercrombie kids
adidas supercolor
asics gel nimbus
stephen curry shoes
louis vuitton outlet online
vans shoes
cartier watches for sale
tiffany jewelry
201671yuanyuan

柯云 said...

2016-07-20keyun
louis vuitton bags
polo ralph lauren
nike uk
coach outlet store online clearances
beats by dre outlet
coach outlet online
louis vuitton outlet online
tods sale
michael kors outlet
cheap air jordans
kd 8
giuseppe zanotti sneakers
cheap jordans
adidas yeezy
burberry handbags
kate spade handbags
michael kors canada outlet
replica rolex watches
michael kors handbags
coach outlet
nike air huarache
nike sb dunks
louis vuitton purses
michael kors outlet
nike store
true religion outlet
coach outlet online
michael kors outlet clearance
christian louboutin wedges
cheap ray ban sunglasses
coach factory outlet
louis vuitton outlet
coach outlet online
louis vuitton handbags
louis vuitton outlet stores
true religion outlet
retro jordans 13
adidas originals store

Unknown said...

thomas sabo uk
herve leger outlet
coach outlet online
louis vuitton handbags
rolex watches
ugg boots
iphone case
cheap snapbacks
uggs outlet
michael kors outlet online
cartier outlet
michael kors outlet clearance
soccer jerseys
oakley sunglasses
celine outlet
canada goose outlet
coach outlet
futbol baratas
nike air force 1
longchamp outlet
coach outlet
michael kors outlet
ugg outlet
canada goose jackets
hermes birkin
polo ralph lauren
michael kors outlet store
air jordan 4
cheap uggs
tory burch outlet
tiffany and co jewelry
louis vuitton outlet
coach outlet
michael kors bags
ugg boots
chanyuan0913

xumeiqing said...

2016923meiqing
canada goose jackets
michael kors handbags
levis outlet online
ugg outlet
canada goose
timberland boots
louis vuitton handbags outlet
doudoune moncler
armani exchange
michael kors outlet clearance

dong dong23 said...

tiffany and co
louis vuitton outlet
clarisonic
true religion jeans
timberland outlet
true religion outlet
louis vuitton outlet
cheap uggs
coach outlet
true religion outlet
mlb jerseys
oakley sunglasses
adidas outlet
michael kors outlet
longchamp handbags
beats by dre
rolex watches
ray ban sunglasses
ugg boots
ugg australia
moncler uk
ray ban sunglasses
louis vuitton paris
toms shoes
louis vuitton handbags
coach factory outlet
moncler uk
gucci outlet
canada goose outlet
adidas nmd
timberland boots
juicy couture
burberry outlet
ugg boots
the north face jackets
timberland boots
new york knicks jerseys
nike outlet store
ugg sale
ugg italia
201611.5wengdongdong

VIMAX said...

For me it is interesting that, since learning I had the ability to start a business online as well as it’s truly great
for me to learn and begin working.

CERITA SEX said...

For me it is interesting that, since learning I had the ability to start a business online as well as it’s truly great
for me to learn and begin working.KLG YOGYAKARTA
PARFUM PERANGSANG
VIMAX
OBAT PEMBESAR PENIS
ALAT BANTU SEX

Obat Sipilis said...

obat bius
obat bius ampuh
obat bius cair
obat bius asli
obat bius terbaik


obat tidur
obat tidur asli
obat tidur ampuh
obat tidur cair


opium spray
opium spray asli
obat perangsang semprot
perangsang wanita semprot
obat perangsang wanita


blue wizard
blue wizard asli
obat perangsang cair
obat perangsang ampuh
obat perangsang asli


perangsang permen karet
perangsang wanita permen karet
perangsang sex love
perangsang permen karet cinta

roshini said...


Sage Support Phone Number
Sage 50 Tech Support Phone Number
Sage 50 Support Number
Sage 50 Technical Support Phone Number
Sage 50 Tech Support Number
Sage 50 Technical Support Phone Number
Sage 50 Technical Support Number
Sage 50 Customer Support Phone Number
Sage 50 Customer Service Number

Soni yadav said...

Aol Technical Support Phone Number
Aol Technical Support Number
Aol Tech Support Number
Aol Tech Support phone Number
Aol Support Phone Number
Aol Tech Support Number
Outlook Technical Support Phone Number
Outlook Technical Support Number
Outlook Tech Support Number
Outlook Tech Support Phone Number
Outlook Support Phone Number
Outlook Tech Support Number

Anjilo Jilo said...


Quicken Support Phone Number
Quicken Support Phone Number
Quicken Support Number
Quicken Tech Support Number
Quicken Tech Support Phone Number
Quicken Technical Support Number
Quicken Technical Support Phone Number
Quicken Technical Support Number
Quicken Tech Support Phone Number
Quicken Tech Support Number

dewi shering 69 said...

I am quite satisfied with what I read here, very interesting and useful, especially for me.
By Obat kuat pria

mohan kumar said...

Quickbooks Support Phone Number
Quickbooks Support Phone Number
Quickbooks Tech Support Number
Quickbooks Support Number
Quickbooks Technical Support Number
Quickbooks Tech Support Phone Number
Quickbooks Technical Support Phone Number
Quickbooks Help Desk Phone Number
Quickbooks Help Desk Number
Quickbooks Customer Service Number
Quickbooks Customer Service Phone Number
Quickbooks Customer Support Number
Quickbooks Customer Support Phone Number

raybanoutlet001 said...

cheap jordan shoes
nike roshe run
titans jersey
nike air huarache
denver broncos jerseys
michael kors handbags outlet
salvatore ferragamo
michael kors handbags
moncler jackets
michael kors handbags outlet

Unknown said...

nike huarache
north face
michael kors outlet
lebron james shoes
ralph lauren outlet
michael kors handbags sale
new balance shoes
supra shoes sale
michael kors handbags
michael kors handbags

caiyan said...

tommy hilfiger outlet
hollister clothing store
michael kors handbags outlet
ralph lauren outlet
jerseys cheap
fitflops clearance
adidas sneakers
christian louboutin outlet
polo ralph lauren
birkenstock shoes
0413shizhong

samsul jos said...

jual hammer of thor asli
hammer of thor asli
ciri ciri titan gel asli
titan gel
jual titan gel asli
titan gel asli
agen titan gel asli
titan gel original

samsul jos said...



obat bius asli
obat bius
obat bius cair
ciri ciri obat bius asli
obat bius
obat bius serbuk
obat bius
obat bius semprot
obat bius
obat bius hirup

dong dong23 said...

adidas originals tubular
pandora charms
coach handbags
louboutin pas cher
louboutin heels
michael kors handbags
rolex replica watches
vibram fivefingers hurt
adidas yeezy
pandora outlet store
2017.6.27chenlixiang

ninest123 said...

vans outlet store
golden state warriors jerseys
adidas nmd
swarovski crystal
toms shoes
pandora jewelry
michael kors handbags
true religion outlet
michael kors outlet
cheap jerseys from china
mt0718

aaa kitty20101122 said...

jordans for cheap
tory burch shoes
http://www.kobebasketballshoes.us.com
adidas store
adidas online shop
nike huarache
nike polo shirts
ultra boost
michael kors outlet
yeezy boost

alice asd said...

cheap oakley sunglasses
kate spade outlet
michael kors outlet
ed hardy
coach outlet online
coach factory outlet
browns jerseys
ugg boots
polo ralph lauren outlet
ugg boots

Hammer Of Thor said...

Makasih infonya gan, moga artikelnya bermanfaat bagi semua orang......

mampir juga di lapak ane gan,......

- Hammer Of Thor

- Jual Hammer Of Thor

- Hammer Of Thor Asli

- Jual Hammer Of Thor Asli

- Jual Hammer Of Thor Di Semarang

Jual Hammer Of Thor Asli Di Semarang

JUAL OBAT ABORSI TANGERANG said...


This blog is a great source of information which is very useful for me.

Jual Obat Aborsi bandung

Jual Obat Aborsi Pekanbaru

Jual Obat Aborsi Bekasi

Jual Obat Aborsi malang

Jual Obat Aborsi

Obat Aborsi semarang

Obat Aborsi yogyakarta

Obat Aborsi solo

Obat Aborsi

-Can be very slow but shows all backlinks along with their PR, Anchor and if it's a Dofollow