Being able to query PHP arrays is actually very handy! Let's say you have a mixed array containing Employee objects and some other data types. Want only Employee objects? Try this one!
$result = from('$employee')->in($employees)
Want to know if there's any employee age 12? Easy! The following query returns true/false:
$result = from('$employee')->in($employees)->any('$employee => $employee->Age == 12');
Let's do something a little more advanced... Let's fetch all posts on my blog's RSS feed, order them by publication date (descending), and select an anonymous type containing title and author. Here's how:
$rssFeed = simplexml_load_string(file_get_contents('/syndication.axd'));
$result = from('$item')->in($rssFeed->xpath('//channel/item'))
->orderByDescending('$item => strtotime((string)$item->pubDate)')
"Title" => (string)$item->title,
"Author" => (string)$item->author
Download LINQ for PHP (PHPLinq) now and get familiar with it. Since I started working on PHPLinq, I also noticed LINQ implementations for other languages:
This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.