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



23 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')

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

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

Unknown 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

Unknown 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

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

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

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

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

Unknown 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

Unknown 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

Unknown 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

Unknown said...

20180412xiaoke
birkenstock outlet
louis vuitton outlet store
pandora charms sale clearance
michael kors handbags
pandora outlet
replica rolex
ugg outlet
ugg boots clearance
christian louboutin shoes
oakley sunglasses sale

daun bungkus papua said...

joss

zzyytt said...

asics shoes
nike air vapormax
nike lebron 11
goyard handbags
pureboost
nike air force 1 low
ray ban sunglasses online
nike air max
oakley
harden shoes

Unknown said...

nike cortez women
nike air force 1 low
birkin bag
michael kors outlet
timberland outlet
hogan outlet online
nike mercurial vapor
kayno
adidas ultra boost 3.0
adidas yeezy

Konsultasi - Bunda said...


thanks for sharing 082210743003

Obat Aborsi

Obat Telat Bulan

Obat Penggugur

Jual Obat aborsi terbaik

Obat Aborsi Termurah

Obat Aborsi Terpercaya

Obat Aborsi terbaik

jual Obat aborsi termanjur

jual obat aborsi

obat aborsi aman tuntas

obat aborsi termanjur

5689 said...

zzzzz2018.11.6
ugg boots clearance
canadian goose
pandora charms
ugg boots clearance
pandora outlet
pandora outlet
ralph lauren uk
mbt shoes
louboutin shoes
nike outlet

obat aborsi | obat penggugur kandungan | obat pelancar haid said...

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

jual obat Aborsi bandung

jual obat Aborsi malang

jual obat Aborsi

jual obat Aborsi surabaya

jual obat aborsi surabaya

obat Aborsi tangerang

obat Aborsi bali

gem1musi said...

دانلود آهنگ سالگرد ازدواج

دانلود آهنگ های شاد تالار عروسی

آهنگ های رضا کرد
آهنگ های میلاد قربانی

آهنگ تولدت مبارک عشقم

دانلود نوحه

آهنگ های پرطرفدار جدید

آهنگ های اوزیر مهدی زاده
آهنگ های رضا مریدی
هات بت