2016-05-19 5 views
1

私はsolrクエリを実行します。これはジョブの結果セットを返します。文書の並び順を並べ替え

一部のジョブは重複していますが(ソースは異なる)、ジョブのタイトル、説明、場所が同じかどうかによって決定されます。

私はその1つのジョブが複数の情報源を持って...何かのように、私の結果セットをループにしたい、と一つの仕事に複製いずれかを組み合わせた:

オリジナル結果:

$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クエリを実行するためにSolariumを使用しています)

答えて

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)を初期化し、ジョブ配列をループします。空の配列は、タイトルがキーとして使用されているジョブを格納します。ジョブのタイトルが結果配列に存在しない場合は、追加します。ジョブのタイトルがジョブ配列に存在する場合、ジョブは既存のジョブ内の配列(キー 'other_sources'の下に)に追加されます

+0

これは私が尋ねたものとまったく同じでした。試してみた、ありがとう。しかし、私が必要とするのは、solrクエリ自体でこれを行う方法です – rpsep2

関連する問題