2015-08-24 46 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

我知道这是一个整数,但它返回了我的错误 –