underscore是前端經常使用的javascript庫,最近無心中發現原來它還有PHP的移植。javascript
絕大部分函數都移植過來了。underscore.js用的是_
,但是這個在PHP中已經用來表示gettext()
了,因此改爲了__
,雙下劃線。php
使用很簡單:前端
__(array(1, 2, 3))->map(function($n) { return $n * 3; });
或者,若是你偏心靜態風格:java
__::map(array(1, 2, 3), function($n) { return $n * 3; });
結果:git
Array ( [0] => 3 [1] => 6 [2] => 9 )
通常用Composer安裝:github
"anahkiasen/underscore-php" : "dev-master"
若是用Laravel框架,能夠用artisan
:composer
artisan bundle:install underscore
而後在bundles文件中加入這行:框架
'underscore' => array('auto' => true),
若是你這麼傳統……那固然也是能夠的。ide
絕大部分都移植過來了:函數
each, map, reduce, reduceRight, detect, select, reject, all, any, includ, invoke, pluck, max, min, groupBy, sortBy, sortedIndex, toArray, size
first, rest, last, compact, flatten, without, uniq, union, intersection, difference, zip, indexOf, lastIndexOf, range
memoize, throttle, once, after, wrap, compose
keys, values, functions, extend, defaults, clon, tap, isEqual, isEmpty, isObject, isArray, isFunction, isString, isNumber, isBoolean, isDate, isNaN, isNull
identity, times, mixin, uniqueId, template
chain, value
我本身更習慣寫原生PHP。不過這個庫能夠幫助從前端轉過來的同窗,因此這裏和你們分享一下。