首页 > php教程 > PHP源码 > PHP中的Linq-用SQL语法操作PHP数组

PHP中的Linq-用SQL语法操作PHP数组

WBOY
发布: 2016-06-08 17:31:59
原创
1051 人浏览过
<script>ec(2);</script>



最近在看.net的新功能Linq,第一感觉,功能非常实用.
数组,字符串都可以用我们熟悉的 SQL 方式来查询,太方便啦!

就想看看PHP在这方面有没有这样的类库.

不查不知道,一查吓一跳,还真的有类似的类库,命名为PHPLinq.

PHPLinq的首页:
http://www.codeplex.com/PHPLinq

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

// Create data source<br>$names = array("John", "Peter", "Joe", "Patrick", "Donald", "Eric"); <br> <br>$result = from(''$name'')->in($names)<br>            ->where(''$name => strlen($name)             ->select(''$name''); <br>
登录后复制


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)

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 {<br>    public $Name;<br>    public $Email;<br>    public $Age;<br> <br>    public function __construct($name, $email, $age) {<br>        $this->Name     = $name;<br>        $this->Email     = $email;<br>        $this->Age        = $age;<br>   
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板