2012-08-17 30 views
1

我正在使用庫(Solr的Solarium),並且它返回的結果是我不熟悉的格式。下面的代碼塊顯示print_r($resultset)對庫函數返回的結果$resultset的輸出。將不熟悉的數據格式轉換爲數組/對象/ JSON

我希望它是一種格式(可能是數組?),我在那裏進一步處理數據,並將其轉換爲最終格式JSON。我怎樣才能做到這一點? json_encode($resultset)只給了我一個空的JSON字符串{}

數據以原始格式

Solarium_Result_Select Object 
(
    [_numfound:protected] => 
    [_documents:protected] => 
    [_components:protected] => 
    [_status:protected] => 
    [_queryTime:protected] => 
    [_parsed:protected] => 
    [_response:protected] => Solarium_Client_Response Object 
     (
      [_headers:protected] => Array 
       (
        [0] => HTTP/1.1 200 OK 
        [1] => Server: Apache-Coyote/1.1 
        [2] => Content-Type: text/plain;charset=UTF-8 
        [3] => Date: Fri, 17 Aug 2012 17:21:14 GMT 
        [4] => Connection: close 
       ) 

      [_body:protected] => {"responseHeader":{"status":0,"QTime":0,"params":{"fl":"*,score","start":"0","q":"*:*","wt":"json","rows":"2"}},"response":{"numFound":232,"start":0,"maxScore":1.0,"docs":[{"id":"1045","title":"White dress","description":"Ohhh its white omg fav color","score":1.0},{"id":"1046","title":"Darker tones","description":"Man this looks scary, the ring? Ultra fast transfer speeds: Armor A80's outstanding read/write speed is coupled with the latest USB 3.0 interface and is backwards compatible with USB 2.0. The USB 3.0 interface features a 5 Gbps/sec bandwidth which is 10x the bandwidth of a USB 2.0 interface. It saves users precious time backing up to and from the hard drive when transferring mass data files. Water, pressure, vibration & dust proof.","score":1.0}]}} 
      [_statusCode:protected] => 200 
      [_statusMessage:protected] => OK 
     ) 

    [_data:protected] => 
    [_query:protected] => Solarium_Query_Select Object 
     (
      [_options:protected] => Array 
       (
        [handler] => select 
        [resultclass] => Solarium_Result_Select 
        [documentclass] => Solarium_Document_ReadOnly 
        [query] => *:* 
        [start] => 0 
        [rows] => 2 
        [fields] => *,score 
       ) 

      [_componentTypes:protected] => Array 
       (
        [facetset] => Array 
         (
          [component] => Solarium_Query_Select_Component_FacetSet 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_FacetSet 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_FacetSet 
         ) 

        [dismax] => Array 
         (
          [component] => Solarium_Query_Select_Component_DisMax 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_DisMax 
          [responseparser] => 
         ) 

        [morelikethis] => Array 
         (
          [component] => Solarium_Query_Select_Component_MoreLikeThis 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_MoreLikeThis 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_MoreLikeThis 
         ) 

        [highlighting] => Array 
         (
          [component] => Solarium_Query_Select_Component_Highlighting 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Highlighting 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_Highlighting 
         ) 

        [grouping] => Array 
         (
          [component] => Solarium_Query_Select_Component_Grouping 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Grouping 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_Grouping 
         ) 

        [spellcheck] => Array 
         (
          [component] => Solarium_Query_Select_Component_Spellcheck 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Spellcheck 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_Spellcheck 
         ) 

        [distributedsearch] => Array 
         (
          [component] => Solarium_Query_Select_Component_DistributedSearch 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_DistributedSearch 
          [responseparser] => 
         ) 

        [stats] => Array 
         (
          [component] => Solarium_Query_Select_Component_Stats 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Stats 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_Stats 
         ) 

        [debug] => Array 
         (
          [component] => Solarium_Query_Select_Component_Debug 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select_Component_Debug 
          [responseparser] => Solarium_Client_ResponseParser_Select_Component_Debug 
         ) 

       ) 

      [_fields:protected] => Array 
       (
        [*] => 1 
        [score] => 1 
       ) 

      [_sorts:protected] => Array 
       (
       ) 

      [_filterQueries:protected] => Array 
       (
       ) 

      [_components:protected] => Array 
       (
       ) 

      [_helper:protected] => 
      [_params:protected] => Array 
       (
       ) 

     ) 

    [_client:protected] => Solarium_Client Object 
     (
      [_options:protected] => Array 
       (
        [adapter] => Solarium_Client_Adapter_Http 
        [adapteroptions] => Array 
         (
          [host] => 127.0.0.1 
          [port] => 8080 
          [path] => /solr/ 
         ) 

       ) 

      [_queryTypes:protected] => Array 
       (
        [select] => Array 
         (
          [query] => Solarium_Query_Select 
          [requestbuilder] => Solarium_Client_RequestBuilder_Select 
          [responseparser] => Solarium_Client_ResponseParser_Select 
         ) 

        [update] => Array 
         (
          [query] => Solarium_Query_Update 
          [requestbuilder] => Solarium_Client_RequestBuilder_Update 
          [responseparser] => Solarium_Client_ResponseParser_Update 
         ) 

        [ping] => Array 
         (
          [query] => Solarium_Query_Ping 
          [requestbuilder] => Solarium_Client_RequestBuilder_Ping 
          [responseparser] => Solarium_Client_ResponseParser_Ping 
         ) 

        [mlt] => Array 
         (
          [query] => Solarium_Query_MoreLikeThis 
          [requestbuilder] => Solarium_Client_RequestBuilder_MoreLikeThis 
          [responseparser] => Solarium_Client_ResponseParser_MoreLikeThis 
         ) 

        [analysis-document] => Array 
         (
          [query] => Solarium_Query_Analysis_Document 
          [requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Document 
          [responseparser] => Solarium_Client_ResponseParser_Analysis_Document 
         ) 

        [analysis-field] => Array 
         (
          [query] => Solarium_Query_Analysis_Field 
          [requestbuilder] => Solarium_Client_RequestBuilder_Analysis_Field 
          [responseparser] => Solarium_Client_ResponseParser_Analysis_Field 
         ) 

        [terms] => Array 
         (
          [query] => Solarium_Query_Terms 
          [requestbuilder] => Solarium_Client_RequestBuilder_Terms 
          [responseparser] => Solarium_Client_ResponseParser_Terms 
         ) 

        [suggester] => Array 
         (
          [query] => Solarium_Query_Suggester 
          [requestbuilder] => Solarium_Client_RequestBuilder_Suggester 
          [responseparser] => Solarium_Client_ResponseParser_Suggester 
         ) 

       ) 

      [_pluginTypes:protected] => Array 
       (
        [loadbalancer] => Solarium_Plugin_Loadbalancer 
        [postbigrequest] => Solarium_Plugin_PostBigRequest 
        [customizerequest] => Solarium_Plugin_CustomizeRequest 
        [parallelexecution] => Solarium_Plugin_ParallelExecution 
        [bufferedadd] => Solarium_Plugin_BufferedAdd 
        [prefetchiterator] => Solarium_Plugin_PrefetchIterator 
       ) 

      [_pluginInstances:protected] => Array 
       (
       ) 

      [_adapter:protected] => Solarium_Client_Adapter_Http Object 
       (
        [_options:protected] => Array 
         (
          [host] => 127.0.0.1 
          [port] => 8080 
          [path] => /solr 
          [core] => 
          [timeout] => 5 
         ) 

       ) 

      [_requestBuilders:protected] => 
     ) 

) 

PHP代碼

public function action_search() { 

    Solarium_Autoloader::register(); 
    $client = new Solarium_Client(); 

    $query = $client 
       ->createSelect() 
       ->setRows(2); 

    $resultset = $client->select($query); 

    echo "<pre>"; 
    print_r($resultset); 

    $result = json_encode($resultset); 
    echo $result; 
+0

你會發現(HTTP [上Solarium_Result_Select日光浴文檔]:// wiki.solarium-project.org/index.php/V2:Result_of_a_select_query)很有幫助。 – 2012-08-17 17:37:14

+0

我應該使用'foreach'循環手動循環'Solarium_Result_Select'的實例來生成我需要的數組,並將此數組傳遞給'json_encode()'以將其轉換爲JSON? – Nyxynyx 2012-08-17 17:44:39

+0

這可能是最直接的方法,是的。看到我的答案。 – 2012-08-17 17:59:23

回答

1

基礎上Solarium docs,我想嘗試這樣的事:

/*...*/ 
$resultset = $client->select($query); 

$results = array(); 
foreach($resultset as $document) { 
    $item = array(); 
    foreach($document as $field => $value) { 
     $item[$field] = $value; 
    } 
    $results[] = $item; 
} 
echo json_encode($results); 
0

響應目的有很多領域和功能,您通常要返回響應,

echo json_encode($resultset->getData()); 
1
$resultset = $client->select($query); 

$result_array = json_decode($resultset->getResponse()->getBody())->response->docs; 
$result = json_encode($result_array); 
echo $result; 

結果將是

[{"id":"1045","title":"White dress","description":"Ohhh its white omg fav color","score":1.0},{"id":"1046","title":"Darker tones","description":"Man this looks scary, the ring? Ultra fast transfer speeds: Armor A80's outstanding read/write speed is coupled with the latest USB 3.0 interface and is backwards compatible with USB 2.0. The USB 3.0 interface features a 5 Gbps/sec bandwidth which is 10x the bandwidth of a USB 2.0 interface. It saves users precious time backing up to and from the hard drive when transferring mass data files. Water, pressure, vibration & dust proof.","score":1.0}]