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

本文展示了如何利用PHP Linq简化对字符串数组的筛选操作,通过匿名函数或Lambda表达式实现条件过滤,避免了传统循环和条件检查的繁琐过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. (待续...)

转载地址:http://www.weiqinxue.cn/blogs/index.php/User/articleview/ArticleID/U2A15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值