1

我正在使用一个库(Solarium for Solr),它返回的结果是我不熟悉的格式。下面的代码块显示了库函数返回print_r($resultset)的结果的输出。$resultset

我希望它采用一种格式(也许是数组?),我进一步按摩数据,并将其转换为最终格式 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;
4

3 回答 3

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);
于 2012-08-17T17:58:50.933 回答
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}]
于 2016-02-05T06:27:45.743 回答
0

响应对象有很多字段和函数,你通常想要返回响应,

echo json_encode($resultset->getData());
于 2015-10-21T09:29:02.213 回答