久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      在PHP中處理JSON數(shù)組以及對(duì)象

      在 PHP 中處理 JSON 數(shù)組以及對(duì)象

      與客戶(hù)端混淆的常見(jiàn)原因是圍繞 JSON 數(shù)組和對(duì)象,以及如何在 PHP 中指定他們。特別是,問(wèn)題是由空對(duì)象和數(shù)組對(duì)象引起的,此頁(yè)面將向您展示 Elasticsearch JSON API 中使用的一些常見(jiàn)模式,以及如何將其轉(zhuǎn)換為 PHP 的表現(xiàn)形式。

      空對(duì)象

      Elasticsearch API 在多個(gè)位置上使用空的 JSON 對(duì)象,這可能會(huì)給 PHP 帶來(lái)問(wèn)題。與其他語(yǔ)言不同,PHP 沒(méi)有空對(duì)象的「簡(jiǎn)短」表現(xiàn)形式。因此許多開(kāi)發(fā)人員不知道如何指定空對(duì)象。

      考慮在查詢(xún)中添加突出顯示:

      {     "query" : {         "match" : {             "content" : "quick brown fox"         }     },     "highlight" : {         "fields" : {             "content" : {} (1)         }     } }

      1.這個(gè)空的 JSON 對(duì)象是導(dǎo)致問(wèn)題的原因。

      問(wèn)題是 PHP 會(huì)自動(dòng)轉(zhuǎn)換 "content" : {} 成 "content" : [],這樣將不再是有效的 Elasticsearch DSL。我們需要告訴 PHP 空對(duì)象是一個(gè)顯示對(duì)象,而不是數(shù)組 。 在 PHP 中 定義此查詢(xún), 您可以:

      $params['body'] = array(     'query' => array(         'match' => array(             'content' => 'quick brown fox'         )     ),     'highlight' => array(         'fields' => array(             'content' => new stdClass() (1)         )     ) ); $results = $client->search($params);

      我們使用通用的 PHP stdClass 對(duì)象來(lái)表示一個(gè)空對(duì)象,JSON 將會(huì)被正確解碼。

      通過(guò)使用顯示的 stdClass 對(duì)象,我們可以強(qiáng)制使用 json_encode 解析器正確的輸出空對(duì)象,而不是空數(shù)組。 遺憾的是,這個(gè)冗長(zhǎng)的解決方案,是在 PHP 中實(shí)現(xiàn)目標(biāo)的唯一方法…… 并沒(méi)有空對(duì)象的「簡(jiǎn)短」版本。

      對(duì)象數(shù)組

      Elasticsearch DSL 中的另一種常見(jiàn)模式是對(duì)象數(shù)組。例如,考慮為查詢(xún)添加排序:

      {     "query" : {         "match" : { "content" : "quick brown fox" }     },     "sort" : [  (1)         {"time" : {"order" : "desc"}},         {"popularity" : {"order" : "desc"}}     ] }

      1.「sort」包含一組 JSON 對(duì)象

      這種安排很常見(jiàn),但是 PHP 的結(jié)構(gòu)可能很復(fù)雜。 因?yàn)樗枰短讛?shù)組。PHP 的冗長(zhǎng)往往會(huì)掩蓋實(shí)際發(fā)生的事。要構(gòu)造一個(gè)對(duì)象數(shù)組,實(shí)際上需要一個(gè)數(shù)組數(shù)組 :

      $params['body'] = array(     'query' => array(         'match' => array(             'content' => 'quick brown fox'         )     ),     'sort' => array(    (1)         array('time' => array('order' => 'desc')),  (2)         array('popularity' => array('order' => 'desc')) (3)     ) ); $results = $client->search($params);

      1.該數(shù)組對(duì) "sort" : [] 數(shù)組進(jìn)行編碼

      2.該數(shù)組對(duì) {"time" : {"order" : "desc"}} 對(duì)象進(jìn)行編碼

      3.該數(shù)組對(duì) {"popularity" : {"order" : "desc"}} 對(duì)象進(jìn)行編碼

      如果您使用 5.4+ 以上的版本,我強(qiáng)烈建議你使用短數(shù)組語(yǔ)法。它使這些嵌套數(shù)組更容易閱讀:

      $params['body'] = [     'query' => [         'match' => [             'content' => 'quick brown fox'         ]     ],     'sort' => [         ['time' => ['order' => 'desc']],         ['popularity' => ['order' => 'desc']]     ] ]; $results = $client->search($params);

      空對(duì)象數(shù)組

      有時(shí), 您會(huì)遇到前面兩種模式的 DSL . 這個(gè)得分函數(shù)的查詢(xún)是一個(gè)很好的例子,有時(shí)他需要一個(gè)空的對(duì)象數(shù)組,其中一些對(duì)象可能是空的 JSON 對(duì)象。

      例如這種查詢(xún):

      {    "query":{       "function_score":{          "functions":[             {                "random_score":{}             }          ],          "boost_mode":"replace"       }    } }

      我們可以使用以下 PHP 代碼構(gòu)建它:

      $params['body'] = array(     'query' => array(         'function_score' => array(             'functions' => array(  (1)                 array(  (2)                     'random_score' => new stdClass() (3)                 )             )         )     ) ); $results = $client->search($params);

      1.它對(duì)對(duì)象數(shù)組進(jìn)行編碼: "functions" : []

      2.它對(duì)數(shù)組中的對(duì)象進(jìn)行編碼: { "random_score": {} }

      3.T 它對(duì)空的 JSON 對(duì)象進(jìn)行編碼: "random_score": {}

      推薦:【PHP教程】

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)