最近在看.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 < 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;
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索sql
, php
, 数组
, array
, this
objects
php 数组操作、php 数组操作函数、php数组操作函数汇总、php操作数组的函数、php 数组操作大全,以便于您获取更多的相关知识。