Php 对象数组上的数组_diff
我有一个PHP函数数组_diff的问题 在这两种情况下,我都在相同类对象的数组上使用它 第一种情况:Php 对象数组上的数组_diff,php,arrays,symfony,array-difference,Php,Arrays,Symfony,Array Difference,我有一个PHP函数数组_diff的问题 在这两种情况下,我都在相同类对象的数组上使用它 第一种情况: public function findFreeUsers($weekId) { $em = $this->getEntityManager(); $week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]); $busyWeeks = $em->getReposi
public function findFreeUsers($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyUsers = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(UserWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyUsers[] = $singleWeek->getUser();
}
}
}
$allUsers = $em->getRepository(User::class)->findAll();
$freeUsers = array_diff($allUsers, $busyUsers);
return $freeUsers;
}
第二种情况:
public function findFreeCars($weekId)
{
$em = $this->getEntityManager();
$week = $em->getRepository(Week::class)->findOneBy(["id" => $weekId]);
$busyWeeks = $em->getRepository(Week::class)->findWeekBetweenDates($week);
$busyCars = array();
foreach ($busyWeeks AS $busyWeek) {
$tmp = $em->getRepository(CarWeek::class)->findBy(["week" => $busyWeek["id"]]);
if ($tmp != null) {
foreach($tmp AS $singleWeek) {
$busyCars[] = $singleWeek->getCar();
}
}
}
$allCars = $em->getRepository(Car::class)->findAll();
$freeCars = array_diff($allCars, $busyCars);
return $freeCars;
}
我正在转储这些数组,它们都是具有相同类对象的数组
在第一种情况下有效,在第二种情况下我得到:
错误:类AppBundle\Entity\Car的对象无法转换为字符串
您不应该使用array_diff将数组与对象进行比较 要正确地执行此操作,您需要使用并定义对象之间的“差异”含义 例如,如果对象具有不同的id,则对象可能不同
function compareCars(Car $objA, Car $objB) {
return $objA->getId() <=> $objB->getId();
}
$diff = array_udiff($allCars, $busyCars, 'compareCars')
功能比较器(车$objA,车$objB){
返回$objA->getId()$objB->getId();
}
$diff=array\u udiff($allCars、$busyCars、'compareCars'))
例如,如果您将ComparableInterface添加到每个要通过id与一个方法getId()进行比较的类中,那么您可以利用多态性的好处
interface ComparableInterface
{
public function getId();
}
class Car implements ComparableInterface
{
public function getId()
{
return $this->id;
}
//rest of the class source
}
function compareCars(ComparableInterface $objA, ComparableInterface $objB) {
return $objA->getId() <=> $objB->getId();
}
接口可比接口
{
公共函数getId();
}
类Car实现可比接口
{
公共函数getId()
{
返回$this->id;
}
//类源的其余部分
}
功能比较卡(可比接口$objA,可比接口$objB){
返回$objA->getId()$objB->getId();
}
或者甚至定义compare()方法,该方法为每个对象返回相等或不相等的值
interface AdvancedComparableInterface extends ComparableInterface
{
public function compare(ComparableInterface $obj);
}
class Car implements AdvancedComparableInterface
{
public function getId()
{
return $this->id;
}
public function compare(ComparableInterface $obj)
{
return $this->getId() <=> $obj->getId();
}
//rest of the class source
}
function compareCars(AdvancedComparableInterface $objA, ComparableInterface $objB) {
return $objA->compare($objB);
}
接口高级可比接口扩展可比接口
{
公共功能比较(可比接口$obj);
}
类Car实现了AdvancedComparableInterface
{
公共函数getId()
{
返回$this->id;
}
公共功能比较(可比接口$obj)
{
返回$this->getId()$obj->getId();
}
//类源的其余部分
}
功能比较卡(高级可比接口$objA,可比接口$objB){
返回$objA->compare($objB);
}
如您所见,您可以使用多种方式定义对象是否与另一个相同。例如,可以通过VIN来比较您的汽车
旁注:
就条令的性能而言,在循环中执行查询是个坏主意。最好通过将busyWeek的ID作为数组传递给findBy方法来进行一次查询 您能给出两个数组的示例响应吗?请阅读这里