After working with Ruby full time for over a month, I went back to a PHP project and decided something was missing...
This project aims to port some of the lovely ruby methods for working with arrays and hashes to PHP.
$myArray = Ruby::wrap(array(1,2,3));
$sum = $myArray->inject(0, function($sum,$i){return $sum + $i});
echo $sum; // prints 6
There are implementations for many of the common methods - scroll down to see examples.
There are two ways to use the code: you can treat it as a set of helper methods or as an array wrapper.
Ruby::method($myArray, function($args){ #evaluate });
$a = Ruby::collection();
# or
$a = Ruby::wrap($myArray);
# both
$a->method(function($args){ #evaluate });
The Helper method style is a little more verbose but maintains your direct access to the array:
$myArray['someKey'];
The Array wrapper style is neater but more verbose for array access:
$myArray->fetch('someKey');
# or
$myArray->unwrap()['someKey'];
The following examples assume a wrapped php array containing the values 1,2,3
$a = Ruby::wrap(array(1,2,3));
$a->all(function($i){return is_number($i)}); // TRUE
$a->any(function($i){return $i==1;}); // TRUE
$a->push(NULL); // [1,2,3,NULL]
$a->compact(); // [1,2,3]
$a->collect(function($i){return $i*2;}); // [2,4,6]
$a->each(function($i){echo $i;}); // prints 1 2 3
$a->each_with_index(function($i,$d){echo $i + $d;}) // prints 1 3 5
$a->inject(0, function($sum,$i){return $sum + $i;}); // 6
$a->join(','); // '1,2,3'
$a->reject(function($i){return $i%2==0;}); // [1,3]
$a->select(function($i){return $i%2==0;}); // [2]