【问题标题】:YaLinqo - Get correct result for where clause containing equals operatorYaLinqo - 获取包含等于运算符的 where 子句的正确结果
【发布时间】:2015-10-07 09:49:04
【问题描述】:

我正在尝试使用类似 SQL 的语法查询一个数组,并且我正在了解 YaLinqo。

我设法让它适用于具有优越或劣等运算符的 where 子句,但我不能让它与等于运算符一起使用。

我做错了什么?

这里是一个例子:

require_once __DIR__ . '/vendor/autoload.php';

use \YaLinqo\Enumerable;

$users = [
    [
        'UserId' => '1',
        'username' => 'joe',
        'password' => 'joepw',
        'mail' => 'joe@mail.com'
    ],
    [
        'UserId' => '2',
        'username' => 'nancy',
        'password' => 'nancypw',
        'mail' => 'nancy@mail.com'
    ],
    [
        'UserId' => '3',
        'username' => 'alice',
        'password' => 'alicepw',
        'mail' => 'alice@mail.com'
    ]
];

$working = \YaLinqo\Enumerable::from($users)
    ->where('$users ==> $users["UserId"] > 2')
    ->toArray();

$notWorking = \YaLinqo\Enumerable::from($users)
    ->where('$users ==> $users["UserId"] = 2')
    ->toArray();

$workingAndUgly = \YaLinqo\Enumerable::from($users)
    ->where('$users ==> $users["UserId"] > 1')
    ->where('$users ==> $users["UserId"] < 3')
    ->toArray();

$notWorkingEither = \YaLinqo\Enumerable::from($users)
    ->where('$users ==> $users["username"] = "nancy"')
    ->toArray();

var_dump($working, $notWorking, $workingAndUgly, $notWorkingEither);

【问题讨论】:

    标签: php arrays linq where-clause yalinqo


    【解决方案1】:

    我发现使用== 而不是= 可以:

    // previously $notWorking
    $nowWorking = \YaLinqo\Enumerable::from($users)
        ->where('$users ==> $users["UserId"] == 2')
        ->toArray();
    
    // previously $notWorkingEither
    $nowWorkingAlso = \YaLinqo\Enumerable::from($users)
    ->where('$users ==> $users["username"] == "nancy"')
    ->toArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-27
      • 2011-01-10
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多