douhu5837 2017-04-12 19:16
浏览 32
已采纳

如何比较一个对象数组,并在匹配时合并共享属性的值?

Background

In a project I am working on, I have created a Builder class with a few methods that builds its objects in different ways. Let's call it CompanyBuilder.

CompanyBuilder has three objects its in charge of building: Company, Employee and Skill. Inside each assembly method it injects some dependencies inside each object.

Objects

These are the objects its in charge of building, I realize there's four here, but there are three main objects.

class Company {
    public $name;
    public $employees = array();

    public function __construct($name) {
        $this->name = $name;
    }
}

class Person {
    public $name;
}

class Employee extends Person {
    public $job;
    public $skills = array();
}

class Skill {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }
}

Builder

There are three methods in the builder aimed to provide flexibility when building the objects. The one that generated this question looks like this:

$company = $companyBuilder->buildCompany(
    'Acme',
    $dependency,
    $employee,
    array(
        $skillOne,
        $skillTwo,
        $skillThree
    )
);

The result of this would be one company object, with one employee which has one or many skills. This approach meets a business goal. However, there is a problem.

The Problem

The problem arises when I have different Company objects, that contain the same name property. I want to merge the employees into one Company object, if the Company names match.

I had the thought I could create a mergeEmployees method, that could take in an indeterminate amount of Company objects as arguments, that are captured using func_get_args(), which turns them into an array of objects. I've tried this solution and it hasn't worked for me.

I want to find a company, in that array, with the same name and merge it's employees. So if the args array looks like this:

Company Object
(
    [name] => Acme
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Designer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => web
                                )

                            [1] => Skill Object
                                (
                                    [name] => ui
                                )

                        )

                    [name] => Jacob
                )

        )

)
Company Object
(
    [name] => Scholastic
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Developer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => java
                                )

                            [1] => Skill Object
                                (
                                    [name] => c#
                                )

                        )

                    [name] => Steve
                )

        )

)
Company Object
(
    [name] => Acme
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Designer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => ux
                                )

                            [1] => Skill Object
                                (
                                    [name] => css
                                )

                        )

                    [name] => Jacob
                )

        )

)

How would I do comparisons on this array of objects, find that the Employee Jacob should have his skills of 'UX' and 'CSS' and merge it (reliably) with those skills of his previous Employee object?

I have tried using array_reduce() but it wasn't working for me --- could definitely have been misusing it since it's intended to:

Iteratively reduce the array to a single value using a callback function

I would want to use array_filter() but it only passes me one item in the callback function, which isn't enough for a comparison. Unless I'm missing something.

What's the best way to do something like this?

  • 写回答

1条回答 默认 最新

  • dousheyan0375 2017-04-12 19:40
    关注

    I would suggest an implementation already in the buildCompany method:

    function buildCompany($companyName, $employeeName, $skills){
        //let's say existing companies are in $companies array
        $existingCompany = false;
    
        foreach($companies as $key => $company){
            if($company->name === $companyName){
                $existingCompany = true;
    
                $existingEmployee = false;
                foreach($company->employees as $keyE => $employee){
                    if($employee->name === $employeeName){
                        $existingEmployee = true;
                        $employee->skills = array_unique(array_merge($employee->skills, $skills), SORT_REGULAR); //http://stackoverflow.com/questions/13469803/php-merging-two-array-into-one-array-also-remove-duplicates
                        break;
                    }
                }
    
                if(!$existingEmployee){
                    $company->employees[] = new Employee($employeeName, $skills); // add a constructor to the Employee, i see it's missing (or just not provided)
                }
            }
        }
        if(!$existingCompany){
            $temp = new Company($companyName);
            $temp->employees[] = new Employee($employeeName, $skills); // add a constructor to the Employee, i see it's missing (or just not provided)
            $companies[] = $temp;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。