Laravel比较两个查询结果集

时间:2018-05-17 02:20:58

标签: php laravel collections

我希望得到这两个查询之间的区别,但它似乎不起作用。

[
   {"subjectcodeco":"BIO 111","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"BIO 112","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"BIO 113","subjectunitco":2,"subjectvalueco":"C"},
   {"subjectcodeco":"BIO 114","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"BIO 115","subjectunitco":2,"subjectvalueco":"C"},
   {"subjectcodeco":"CHE 111","subjectunitco":2,"subjectvalueco":"C"},
   {"subjectcodeco":"CHE 112","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"CHE 113","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"CHE 114","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"EDU 112","subjectunitco":2,"subjectvalueco":"C"},
   {"subjectcodeco":"EDU 113","subjectunitco":2,"subjectvalueco":"C"},
   {"subjectcodeco":"GSE 111","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"GSE 112","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"GSE 113","subjectunitco":1,"subjectvalueco":"C"},
   {"subjectcodeco":"EDU 111","subjectunitco":1,"subjectvalueco":"C"}
]

和这个

[
    {"matricno":"15\/1011","subjectcodeco":"BIO 111","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"BIO 112","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"BIO 113","subjectunitco":2,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"BIO 114","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"BIO 115","subjectunitco":2,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"CHE 111","subjectunitco":2,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"CHE 112","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"CHE 113","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"CHE 114","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"EDU 111","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"EDU 112","subjectunitco":2,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"EDU 113","subjectunitco":2,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"GSE 111","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"GSE 112","subjectunitco":1,"subjectvalueco":"C"},
    {"matricno":"15\/1011","subjectcodeco":"GSE 113","subjectunitco":1,"subjectvalueco":"C"}
]

我尝试过使用laravel collection diffAssoc和diffKeys似乎都没有。

2 个答案:

答案 0 :(得分:0)

您可以简单地循环您的第一组,然后将元素与第二组进行比较:

$set1 = collect([...]); // Contents omitted for brevity
$set2 = collect([...]); // Contents omitted for brevity

$diff = [];

$set1->each(function($item, $key) {
    $exists = $set2->where(...)->first(); // Where clause omitted for brevity
    if($exists) {
        array_push($diff, $item);
    }
});

dd($diff); // Will contain the items of $set1 that don't exist in $set2

答案 1 :(得分:0)

如果我的问题中的假设是正确的,那么你可以这样做。

foreach($query as $queries)
{
    foreach($secondQuery as $secondQueries)
    {
        if($queries['subjectcodeco'] == $secondQueries['subjectcodeco'])
        {
            //Condition Here
        }
        //And continue with the conditions
    }
}

每个数组值的每次迭代也将迭代第二个数组内的所有值,因此检查每个数组的所有单个值。