原创 

codeplex:PHP LINQ classes,PHP也可以使用Linq语法操作对象

分类:    1409人阅读    IT小君  2015-01-24 16:35

Examples

Examples can be found in the test package in the latest release.

A basic example

Let\'s say we have an array of strings and want to select only the strings whose length is < 5. The PHPLinq way of achieving this would be the following: 
// Create data source
$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric"); 

$result = from(\'$name\')->in($names)
            ->where(\'$name => strlen($name) < 5\')
            ->select(\'$name\'); 

Feels familiar to SQL? Yes indeed! No more writing a loop over this array, checking the string\'s length, and adding it to a temporary variable. 
You may have noticed something strange... What\'s that $name => strlen($name) < 5 doing? This piece of code is compiled to an anonymous function or Lambda expression under the covers. This function accepts a parameter $name, and returns a boolean value based on the expression strlen($name) < 5. 

An advanced example

There are lots of other examples available in the PHPLinq download, but here\'s an advanced one... Let\'s say we have an array of Employee objects. This array should be sorted by Employee name, then Employee age. We want only Employees whose name has a length of 4 characters. Next thing: we do not want an Employee instance in our result. Instead, the returning array should contain objects containing an e-mail address and a domain name. 
First of all, let\'s define our data source: 
class Employee {
    public $Name;
    public $Email;
    public $Age;

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

$employees = array(
    new Employee(\'Maarten\', \'maarten@example.com\', 24),
    new Employee(\'Paul\', \'paul@example.com\', 30),
    new Employee(\'Bill\', \'bill.a@example.com\', 29),
    new Employee(\'Bill\', \'bill.g@example.com\', 28),
    new Employee(\'Xavier\', \'xavier@example.com\', 40)
); 

Now for the PHPLinq query:
$result = from(\'$employee\')->in($employees)
            ->where(\'$employee => strlen($employee->Name) == 4\')
            ->orderBy(\'$employee => $employee->Name\')
            ->thenByDescending(\'$employee => $employee->Age\')
            ->select(\'new {
                    "EmailAddress" => $employee->Email,
                    "Domain" => substr($employee->Email, strpos($employee->Email, "@") + 1)
                  }\'); 

Again, you may have noticed something strange... What\'s this new { } thing doing? Actually, this is converted to an anonymous type under the covers. new { "name" => "test" } is evaluated to an object containing the property "name" with a value of "test".

Last edited Jan 22, 2008 at 9:55 PM by maartenba, version 4

项目及下载地址:http://phplinq.codeplex.com/

点击广告,支持我们为你提供更好的服务

中小型创意设计服务公司网站模板

网页设计开发公司网站模板

js+css3抽奖转盘旋转点餐代码

响应式咖啡饮品宣传网站模板

canvas炫酷鼠标移动文字粒子特效

现代时尚家具公司网站模板

HTML5 Canvas竖直流动线条背景动画特效

有机水果蔬菜HTML5网站模板

css鼠标跟随文字模糊特效

html5 canvas进度条圆环图表统计动画特效

HTML5现代家居装潢公司网站模板

html5 canvas彩色碎片组合球形旋转动画特效

html5 svg夜空中星星流星动画场景特效

css+js实现的颜色渐变数字时钟动画特效

响应式时尚单品在线商城网站模板

响应式太阳能能源公司网站模板

jQuery右端悬浮带返回顶部特效

html5图标下拉搜索框自动匹配代码

HTML5数字产品服务公司网站模板

小众时尚单品在线电子商务网站模板

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务