删除PHP中具有Apache_Solr_Document对象的数组中的重复项

时间:2014-04-08 06:02:11

标签: php arrays solr

更新:我有两个独立的数组,用于保存SolrPhpClient的search()函数返回的一些数据。数组包含Apache_Sole_Document对象形式的数据。这些对象又包含实际的字段和值。我合并这两个数组,以使用PHP的array_merge()获取包含所有项目的单个数组

该数组将包含一些需要删除的重复项。 我不知道如何在这个结构中实现它。

数组结构是这样的:

Array ( [0] => Apache_Solr_Document Object ( 
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 111 [name] => ABCD ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        ) 
        [1] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 222 [name] => DEFG ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [2] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 333 [name] => LMNO ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [3] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 111 [name] => ABCD ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [4] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 444 [name] => PQRS ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [5] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 222 [name] => DEFG ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )       
    )

如您所见,有一个[id]字段和一个[name]字段。

我想从比较[id]字段的数组中删除重复项。

删除重复项后的最终数组应如下所示:

Array ( [0] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 111 [name] => ABCD ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        ) 
        [1] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 222 [name] => DEFG ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [2] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 333 [name] => LMNO ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )
        [3] => Apache_Solr_Document Object (
            [_documentBoost:protected] => 
            [_fields:protected] => Array ( [id] => 444 [name] => PQRS ) 
            [_fieldBoosts:protected] => Array ( [id] => [name] => ) 
        )   
    )

我怎样才能做到这一点?有人请帮忙!

1 个答案:

答案 0 :(得分:0)

我想你可以迭代数组并删除重复项,但这对我来说似乎不是一个简洁的解决方案。

我不熟悉SolrPhpClient,但Solr确实支持对字段进行分组。您可以通过将此部分添加到您的请求中来对ID进行分组:

group=true&group.field=id

返回的文件将按ID分组。有关详细信息,请查看this页。

<强>更新

我查看了SolrPhpClient文档,看到您可以为您的请求添加其他参数,如下所示:

$additionalParameters = array(
      'fq' => 'a filtering query',
      'facet' => 'true',
      'facet.field' => array(
           'field_1',
           'field_2'
   )
);
$results = $solr->search($query, $start, $rows, $additionalParameters);

我假设您可以将分组参数添加到此:

$additionalParameters = array(
   'group' => 'true',
   'group.field' => 'id'
    )
);
$results = $solr->search($query, $start, $rows, $additionalParameters);

有关详细信息,请查看this