2015-08-24 94 views
0

我有一個搜索查詢,執行搜索查詢後,我把$結果放入一個數組中。注意:未定義索引:點擊

我的PHP代碼 -

$contents = $client->search($params); // executing the search 

$search = array($contents); // make the result $contents as a array 
for ($i = 0; $i < count($search); $i++) { 
$search['hits']['total']['title'] = strip_tags($search['hits']['total']['title']); // Trying to access the title 

結果的陣列----

Array ( 
    [0] => Array ( 
     [took] => 1 
     [timed_out] => 
     [_shards] => Array ( 
      [total] => 2 
      [successful] => 2 
      [failed] => 0 
     ) 
     [hits] => Array (  
      [total] => 1 
      [max_score] => 2.6818755 
      [hits] => Array ( 
       [0] => Array ( 
        [_index] => myindex 
        [_type] => mytype 
        [_id] => p36d3742b982586d8d 
        [_score] => 2.6818755 
        [_source] => Array ( 
         [title] => Salma Hayek => Salma Hayeks... 
         [source] => Hello 
         [guid] => p36d3742b982586d8d 
         [pub_id] => 54ae51e5 
         [type] => news 
        ) 
       ) 
      ) 
     ) 
    ) 
) 

現在的問題是每一次我嘗試訪問標題時,它說注意: 未定義索引:匹配

我試圖像---

$search['hits']['total']['title'] = strip_tags($search['hits']['total']['title']); 

$search['']['hits']['total']['title'] = strip_tags($search['']['hits']['total']['title']); 

沒有什麼工作對我來說,可能是想提出一個簡單的錯誤,任何人都知道我在哪裏做的錯誤。

我也曾嘗試使用它,像這樣----

$search[$i]['hits']['total']['title'] = strip_tags($search[$i]['hits']['total']['title']); 

但它返回我的錯誤,如:

警告:無法使用標量值作爲數組

如何訪問我的標題?

+0

如果你傾倒在可讀的方式,你會更容易找到該變量的正確路線陣列。 – Popnoodles

回答

2

您缺少$i,請使用$search[$i]['hits']

這是標題$search[$i]['hits']['hits'][0]['_source']['title']

+0

感謝您的回答,但如果我這樣做,它會返回我「警告:不能使用標量值作爲數組」! –

+0

使用$ search [$ i] ['hits'] ['total']不是數組,它是一個整數。 – Pradeep

+0

我知道這是一個整數,但它返回了我的錯誤 –