重复数据库文件

时间:2016-05-19 21:36:22

标签: php solr duplicates

我运行一个solr查询,它返回一个作业结果集。

某些作业是重复的(但来自不同的来源),这取决于作业标题,描述和位置是否相同。

我想循环遍历我的结果集,并将任何重复的内容合并到一个作业中,而这个作业有多个来源......类似于:

原始结果:

$jobs = array(
    array(
        'id'          => 'job1',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source1',
    ),
    array(
        'id'          => 'job2',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source2',
    ),
    array(
        'id'          => 'job3',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source3',
    ),
    array(
        'id'          => 'job4',
        'title'       => 'testing',
        'description' => 'testing',
        'location'    => 'testing',
        'source'      => 'source1',
    )
);

会变成:

$jobs = array(
    array(
        'id'          => 'job1',
        'title'       => 'test',
        'description' => 'test',
        'location'    => 'test',
        'source'      => 'source1',
        'other_sources' => array(
            array(
                'id'          => 'job2',
                'title'       => 'test',
                'description' => 'test',
                'location'    => 'test',
                'source'      => 'source2'
            ),
            array(
                'id'          => job3,
                'title'       => 'test',
                'description' => 'test',
                'location'    => 'test',
                'source'      => 'source3'
            ), 
        ),
    ),
    array(
        'id'          => 'job4',
        'title'       => 'testing',
        'description' => 'testing',
        'location'    => 'testing',
        'source'      => 'source1'
    )
);

我怎样才能做到这一点?无论是在PHP中还是在Solr查询中(我正在使用日光浴进行我的Solr查询)

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

<?php

$result = array();
foreach ($jobs as $job) {

    if (!empty($result[$job['title']])) {
        $result[$job['title']]['other_sources'][] = $job;
    }
    else {
        $result[$job['title']] = $job;
    }

}

初始化一个空数组($result),然后遍历作业数组。空数组将存储作为键的标题作业。如果结果数组中不存在作业标题,则会添加它。如果作业标题中确实存在作业标题,则它会将作业附加到现有作业内的数组中(在键&#39; other_sources&#39;下)