Filter an array of objects

Quite often I might have an array of objects, be it from a db query or some json object, and I want to filter that list in a particular way.  Lots of times I would find myself doing the same old thing; creating a new array, looping and looping until all I had left was what matched my filter.

I’m sure you’ve been there and done it a thousand times, too.

Well, this little function should help that task out a lot!

Say I had an array of people objects and wanted only those people who’s name was Bob and was aged 35, I could do something as simple as:

$filtered = ofilter($items, ['name' => 'Bob', 'age' => 35]);

Or maybe something a little tricker; I wanted to get anyone whose age was between 18 and 35 (inclusive):

$filtered = ofilter($items, ['age' => function($age) { return ($age >= 18 && $age < = 35); }]);

Pretty easy, eh?

Here’s the code – it’s a GitHub gist, so feel free to fork and improve!
Continue reading “Filter an array of objects”

Did you like this? Share it:

Counters in CSS: making a table of contents list

As I’m sure you know, you can create an ordered bullet point list but it has limitations.  For example, if I wanted to have a table of contents so that the numbering goes 1, 1.1, 1.2, 1.2.1, 1.2.2, 1.2.3, 1.2.3.1 and so on then the standard ordered list just isn’t going to cut it.  Oh, sure, you could reset the numbering on the sub-lists so that it uses roman numerals or something like that but to me that just doesn’t feel quite right…

Enter CSS counters!

Counters offer you a way to increment and decrement a value every time an element has the rule, and there doesn’t have to be one counter – you can have as many as you need,

So using counters can easily produce the correct kind of TOC list that we want. And here’s the jsFiddle to show you just how easy it is.

Did you like this? Share it:

Tweeting on a button press with the Raspberry Pi

A while ago my door bell broke. So I did what any sensible person would; I decided that instead of buying a new one I would hook up an old bell I had to a Raspberry Pi and have the bell ring when someone pushed a button on my door. But then I though, “well, that’s a bit boring.  My doorbell should tweet me, too!”

The long and the short of it is that I couldn’t get the mechanics of the bell ringer to work but had a lot of fun with the tweeting side of things because it involved my first shot at doing some Python code. And the really good news is that it’s ridiculously easy to accomplish.

This was roughly my process…

Continue reading “Tweeting on a button press with the Raspberry Pi”

Did you like this? Share it:

Easy page scraping with Zend\Dom (from Zend Framework 2)

The other day I was interested in getting some information from the sussex.academia.edu site, specifically I wanted a list of tags for each of the faculty members. Now, this sounds relatively easy except when you consider that initial page contains a list of links to various schools/departments people have listed, and then under each of those pages you have different fieldsets with different types of people on them (and I was only interested in the faculty fieldset), and each person may or may not have tags and even then those tags may be hidden behind some javascript so that you click and view all of the tags… When you consider all of that you would be forgiven in thinking that it’s actually quite a daunting task!

Let me assure you, though, that by using Zend\Dom from the Zend Framework 2 library it’s actually a really simple task. In fact, I did it in around 20 lines of code.

So let’s start by looking at the code and then break it down a little more.

Continue reading “Easy page scraping with Zend\Dom (from Zend Framework 2)”

Did you like this? Share it:

Handy little function

Quite often I find myself wanting to run the same script by either cli or through a browser. But I don’t want to fill my echo statements with <br /> tags if I’m on cli because that’d just look ugly, but at the same time I don’t just want to use \n when outputting in the browser because everything would be on the same line.

This handy little function helps to do simple output that will be readable in the browser as well as the command line:

$_ = function($str) {
    if (PHP_SAPI == 'cli') {
        echo $str;
    } else {
        echo nl2br(str_replace("\t", str_repeat('&nbsp;', 6), $str))."\n";
        flush();
    }
};

Then when I want to echo something I just do:

$_("This is a test\n");
$_("\tTime:" . time() . "\n\n");

Simple but handy.

Did you like this? Share it:

Sorting an array of objects by one or more object property

Quite often I find myself having an array of objects and needing to sort that array of objects by property (either one property or multiple)…

Imagine, for example, getting a large result set from your database and ordering in the query just takes too long. Or perhaps you’re getting results from a web service and that service doesn’t return the results in the order you’d like to use. Have you ever found yourself in that situation, too? On looking at the usort documentation one day I came across a comment by someone called Will Shaver that did almost what I wanted. With a little adaptation for my own use (being able to change the sort order, for example), it has become one of my favourite functions to use for sorting.

loading gist...

Now a few cools things about the function:

  1. It uses anonymous/lambda functions (or closures, whatever your prefer to call them), and that’s just plain fun
  2. You can sort on more than one property and because the sorting is recursive, it’ll sort the second property within the confines of the first, the third within the confines of the second, and so on. Think sorting in SQL
  3. You can sort in ascending or descending order for any of the properties
  4. It retains key associations so you could use this on an associative array of objects
  5. If the parameter you want to sort on is an array itself then you can use any value (by specifying it’s key) in that array as the sorting value
Did you like this? Share it:

Quick and easy email encoding view helper

Here’s a quick and easy view helper for Zend Framework that will encode an email address. It will encode just an email address or return a whole mailto link. The encoding is basically the same as in the Smarty template engine.

Obviously there’s a lot of room for improvement; javascript encoding, representation as an image, and so on… but then it wouldn’t be quick an easy – it’d be slightly longer and just a little more complex. 😉

Continue reading “Quick and easy email encoding view helper”

Did you like this? Share it:

Auto generating basic models for a Zend Framework app

Do you have a database with foreign keys and just wish you could have something automatically create your ZF models from it? Well, today that was me. So as a little proof of concept, this is the code I came up with to do it for me…

But before we get to that, a few caveats:

  • It’s just a proof of concept
  • The output needs updating for proper reference names, etc.
  • Outputs everything to screen in one go and doesn’t save the files.

However, it might be handy to someone, so I post it up for your comments.
Continue reading “Auto generating basic models for a Zend Framework app”

Did you like this? Share it: