将ArrayCollection与对象数组进行比较 [英] Compare ArrayCollection with array of objects

查看:134
本文介绍了将ArrayCollection与对象数组进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Groupe实体的ManyToMany关系的StatColle实体。
我想将一组Groupe对象与arrayCollection进行比较。

I have a StatColle entity in a ManyToMany relationship with a Groupe entity. I want to compare an array of Groupe objects with an arrayCollection.

这是我的代码:

//Create array of Groupe objects
$nameOfSelectedGroupes = $request->request->get('selectedGroupes');

        $groupes= [];
        foreach ($nameOfSelectedGroupes as $nameOfSelectedGroupe) {
            $groupe = $em->getRepository(Groupe::class)->findBy(['nom' => $nameOfSelectedGroupe]);
            $groupes[] = $groupe;
        }

// Compare array of objects with ArrayCollection of objects
...request to get StatColle...
foreach ($resultats as $resultat)
    {
        if ($groupes == $resultat->getGroupes()->toArray())
        return $resultat;
    }
...

这总是返回null。我认为 $ resultat-> getGroupes() - > toArray()不是将Groups链接到StatColle实体的正确方法。

This is always returning null. I think that $resultat->getGroupes()->toArray() is not the correct way to get Groupes linked to StatColle entity.

你有想法来比较这些数组吗?

Do you have an idea to compare these arrays ?

推荐答案

我想你不能比较像这样的阵列尝试 array_diff ,然后检查结果数组是否为 empty (表示相同):

I think you can't compare arrays like that. Try array_diff, and then check if the resultant array is empty (means they are the same):

foreach ($resultats as $resultat){
    $compare = array_diff($groupes, $resultat->getGroupes()->toArray());
    if (empty($compare)){
        return $resultat;
    }
}

我认为应该工作。

这篇关于将ArrayCollection与对象数组进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆