Book Review - PHP Array Filtering, The Use cases

David Carr

2 min read - 6th Dec, 2013

Do you use arrays? Do you use the most effective inbuilt functions to filter multiple arrays? If not then this book (http://developerpress.com/en/php-array-filtering-1) will be incredibly useful for you!

This book is a fantastic resource its almost like a manual for array's covering three core areas

1) Difference

"The difference functions are especially useful when you have more than two arrays to compare. Compared to the PHP code needed to do the same work, they become a very clean solution."

2) Intersection

"Array intersection is the mirror of array difference. With the difference functions you are finding out what is in an array that is not in any other array. With intersections you will find out what is in this array that is also in all the other arrays."

3) Filtering

"The filtering toolkit. Outside of difference and intersection, these functions will accomplish most all of your filtering needs."

Each chapter covers in detail the array functions with explanations on why you would use one of the array functions with practical examples you can use right away, for instance one of the examples:

Split dataset up by odd and even in display logic

You have a dataset that, for purely display reasons, you want to split up into an odd list and an even list. You often have to slice and dice data when displaying it. Code like the following allows you to do that without changing the original data and without having to mix display logic in with your business logic.

<?php
$resultList = array(
  'Ted' => 11, 'Nick' => 12,
  'Bob' => 32, 'John' => 7 );
$odd = function($value) { return $value % 2 !== 0;
};

$even = function($value) {
??  return $value % 2 === 0;
};

$oddList = array_filter($resultList, $odd); 
$evenList = array_filter($resultList, $even);

echo 'Odd: ', print_r($oddList, 1);
echo 'Even: ', print_r($evenList, 1);

Output:

Odd: Array 
(
  [Ted] => 11
  [John] => 7 
)
Even: Array 
(
  [Nick] => 12
  [Bob] => 32 
)

Conclusion

I would recommend this book to anyone looking for a better understanding of arrays or even to brush up on them, the book is laid out in organised way its very easy to move from examples to example or straight to a particular area of interest.

You can learn more about this book and ways to purchase it at the publishers website Developer.Press

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.