简体   繁体   English

Laravel 集合在嵌套对象上相交

[英]Laravel collection intersect on nested object

I've two collections given below:我在下面给出了两个集合:


$collection1 = collect([
    [13254, 28.2],
    [13332, 23.45],
]);

$collection2 = collect([
    [14673, 14.2],
    [13254, 12.2],
]);

And I expect the output similar to:我希望输出类似于:

[
    [13254, 28.2],
]

I tried using $collection1->intersect($collection2) but that did not seem to work.我尝试使用$collection1->intersect($collection2)但这似乎不起作用。

try this code just i use map试试这个代码,只是我使用地图

$res =  $collectionA->map(function ($item ,$index) use ($collectionB){

        if( (($item[0] == $collectionB[$index][0]) || ($item[1] == $collectionB[$index][1]) ){
            return $item;
        }

    })->filter();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM