Php 将不熟悉的数据格式转换为array/object/JSON

Php 将不熟悉的数据格式转换为array/object/JSON,php,solr,solarium,Php,Solr,Solarium,我正在使用一个库(用于Solr),它返回的结果是我不熟悉的格式。下面的代码块显示了库函数返回的结果$resultset上打印($resultset)的输出 我希望它是一种格式(可能是数组?),在这种格式中,我进一步传递数据,并将其转换为最终的JSON格式。我该怎么做json\u encode($resultset)刚刚给了我一个空的json字符串{} 原始格式的数据 Solarium_Result_Select Object ( [_numfound:protected] =>

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

我希望它是一种格式(可能是数组?),在这种格式中,我进一步传递数据,并将其转换为最终的JSON格式。我该怎么做
json\u 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;
public function action\u search(){
日光浴室自动加载器::寄存器();
$client=新日光浴室客户端();
$query=$client
->createSelect()
->设置行数(2);
$resultset=$client->select($query);
回声“;
打印(结果集);
$result=json_encode($resultset);
回声$结果;
基于,我会尝试以下方法:

echo json_encode($resultset->getData());

响应对象有很多字段和函数,您通常希望返回响应

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

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


您可能会发现有帮助。我是否应该使用
foreach
循环手动循环通过
Solarium\u Result\u Select
的实例来生成所需的数组,并将该数组传递到
json\u encode()
以将其转换为json?这可能是最简单的方法,是的。请参阅我的答案。
$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}]