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 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器