YaLinqo - 获取包含equals运算符的where子句的正确结果

时间:2015-10-07 09:49:04

标签: php arrays linq where-clause yalinqo

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

我设法让它适用于高级或低级操作符的where子句,但我无法使用equals运算符。

我做错了什么?

这是一个例子:

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);

1 个答案:

答案 0 :(得分: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();