Yet another php array helper class

On September 1, 2012, in PHP, by terencejackson

Hi,

in each PHP project you implement you have to handle with arrays. You need some elements of the array or you have to delete some elements of the array or even have to transform the elements of the array. What you need are foreach loops each time you want to use it (If you did’nt encapsulate it in an own class).

Inspired by some fantastic methods of guava iterables, the LINQ extensions methods and the problems mentioned above I decided to create a static array helper class which handles problems like search and delete in a easy to understand syntax.

For a better understanding here are some examples of the array helper class.

For the tests I always use the following class and the following array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Person{
public $age;
public $name;
 
public function __construct($name, $age){
$this->name = $name;
$this->age = $age;
}
}
 
function createDemoArray(){
$array = array();
array_push($array, new Person("Mister X.", 40));
array_push($array, new Person("Mister Y.", 50));
array_push($array, new Person("Mister Z.", 60));
array_push($array, new Person("Mister Y.", 70));
return $array;
}
class Person{
public $age;
public $name;

public function __construct($name, $age){
$this->name = $name;
$this->age = $age;
}
}

function createDemoArray(){
$array = array();
array_push($array, new Person("Mister X.", 40));
array_push($array, new Person("Mister Y.", 50));
array_push($array, new Person("Mister Z.", 60));
array_push($array, new Person("Mister Y.", 70));
return $array;
}

1) Get all person which are younger than 50 without additional parameter:

1
2
3
$result =<strong> Arrays::filter</strong>($array, function($person) {return $person->age <= 50;});
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) [1] => Person Object ( [age] => 50 [name] => Mister Y. ) )
$result =<strong> Arrays::filter</strong>($array, function($person) {return $person->age <= 50;});
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) [1] => Person Object ( [age] => 50 [name] => Mister Y. ) )

2) Get all person which are younger than 49 with additional parameter:

1
2
3
$result = Arrays::filter($array, function($person, $maxAge) {return $person->age <= $maxAge;},49);
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) )
$result = Arrays::filter($array, function($person, $maxAge) {return $person->age <= $maxAge;},49);
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) )

3) Is there a person called “Mister X.” (any):

1
2
3
$result = Arrays::any($array, function($person) { return $person->name == "Mister X."; });
print_r($result);
Result: 1
$result = Arrays::any($array, function($person) { return $person->name == "Mister X."; });
print_r($result);
Result: 1

4):Is there a person called “Mister X.” (any with parameter)

1
2
3
$result = Arrays::any($array, function($person, $name) { return $person->name == $name;}, "Mister X.");
print_r($result);
Result: 1
$result = Arrays::any($array, function($person, $name) { return $person->name == $name;}, "Mister X.");
print_r($result);
Result: 1

5) Get the first person who is called “Mister Y.” (first):

1
2
3
$result = Arrays::first($array, function($person, $name) { return $person->name == $name;}, Mister Y.");
print_r($result);
Result: Person Object ( [age] => 70 [name] => Mister Y. )
$result = Arrays::first($array, function($person, $name) { return $person->name == $name;}, Mister Y.");
print_r($result);
Result: Person Object ( [age] => 70 [name] => Mister Y. )

6)Delete all persons called “Mister Y.” (delete):

1
2
3
$result = Arrays::delete($array, function($person, $name) {return $person->name == $name;}, "Mister Y.");
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) [2] => Person Object ( [age] => 60 [name] => Mister Z.)
$result = Arrays::delete($array, function($person, $name) {return $person->name == $name;}, "Mister Y.");
print_r($result);
Result: Array ( [0] => Person Object ( [age] => 40 [name] => Mister X. ) [2] => Person Object ( [age] => 60 [name] => Mister Z.)

7) Transform all persons to a string array with the names (transform):

1
2
3
$result = Arrays::transform($array, function($person) {return $person->name;});
print_r($result);
Array ( [0] => Mister X. [1] => Mister Y. [2] => Mister Z. [3] => Mister Y. )
$result = Arrays::transform($array, function($person) {return $person->name;});
print_r($result);
Array ( [0] => Mister X. [1] => Mister Y. [2] => Mister Z. [3] => Mister Y. )

As you can see things like looking up an element of an array is a one liner now 😉

You can download the class here.

If you have any suggestions or bug reports, just leave a comment…

Hope you enjoy my little PHP array helper.

Cheers!

Tagged with:  

Leave a Reply

Your email address will not be published. Required fields are marked *