Table of Contents
Examples
A basic example
An advanced example
Home php教程 PHP源码 PHP中的Linq-用SQL语法操作PHP数组

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

Jun 08, 2016 pm 05:31 PM
name quot the this

<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'')-&gt;in($names)<br>            -&gt;where(''$name =&gt; strlen($name)             -&gt;select(''$name''); <br>
Copy after login


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-&gt;Name     = $name;<br>        $this-&gt;Email     = $email;<br>        $this-&gt;Age        = $age;<br>   
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

After 2 months, the humanoid robot Walker S can fold clothes After 2 months, the humanoid robot Walker S can fold clothes Apr 03, 2024 am 08:01 AM

After 2 months, the humanoid robot Walker S can fold clothes

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

What currency is THE? Is THE coin worth investing in? What currency is THE? Is THE coin worth investing in? Feb 21, 2024 pm 03:49 PM

What currency is THE? Is THE coin worth investing in?

How to use this method in Java How to use this method in Java Apr 18, 2023 pm 01:58 PM

How to use this method in Java

An article that understands this point and catches up with 70% of front-end people An article that understands this point and catches up with 70% of front-end people Sep 06, 2022 pm 05:03 PM

An article that understands this point and catches up with 70% of front-end people

How to check the latest price of The Sandbox coin? How to check the latest price of The Sandbox coin? Mar 05, 2024 am 11:52 AM

How to check the latest price of The Sandbox coin?

How to add name to setup in Vue3 How to add name to setup in Vue3 May 13, 2023 am 09:40 AM

How to add name to setup in Vue3

Detailed explanation of this in JavaScript arrow function Detailed explanation of this in JavaScript arrow function Jan 25, 2024 pm 01:41 PM

Detailed explanation of this in JavaScript arrow function

See all articles