查詢

SolrUtils::digestXmlResponse()函式—用法及示例

「 解析Solr XML響應並返回一個關聯陣列 」


函式名稱:SolrUtils::digestXmlResponse()

適用版本:PHP 5.2.9及以上版本

函式描述:該函式用於解析Solr XML響應並返回一個關聯陣列。

用法示例:

$xmlResponse = '<response>
  <lst name="responseHeader">
    <int name="status">0</int>
    <int name="QTime">10</int>
  </lst>
  <result name="response" numFound="1" start="0">
    <doc>
      <str name="id">1</str>
      <str name="title">Example Document</str>
      <str name="content">This is an example document.</str>
    </doc>
  </result>
</response>';

$responseArray = SolrUtils::digestXmlResponse($xmlResponse);

// 列印關聯陣列
print_r($responseArray);

輸出結果:

Array
(
    [responseHeader] => Array
        (
            [status] => 0
            [QTime] => 10
        )

    [response] => Array
        (
            [numFound] => 1
            [start] => 0
        )

    [response] => Array
        (
            [doc] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [title] => Example Document
                            [content] => This is an example document.
                        )
                )
        )
)

解釋:首先,我們定義了一個包含Solr XML響應的字串變數。然後,我們呼叫SolrUtils::digestXmlResponse()函式,並將XML響應作為引數傳遞給它。函式會解析XML,並將其轉換為一個關聯陣列。最後,我們透過print_r()函式列印出關聯陣列的內容。輸出結果顯示了XML響應被成功解析成了一個多維關聯陣列。

補充糾錯
熱門PHP函式
分享連結