【问题标题】:Notice: Undefined index: hits注意:未定义的索引:命中
【发布时间】:2015-08-25 00:09:54
【问题描述】:

我有一个搜索查询,在执行搜索查询后,我将 $result 放入一个数组中。

我的 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']);

但它会返回一个错误,例如:

警告:不能将标量值用作数组

如何访问我的作品?

【问题讨论】:

  • 如果你以一种可读的方式转储你的数组,你会更容易找到到变量的正确路径。

标签: php elasticsearch


【解决方案1】:

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

这里的标题是$search[$i]['hits']['hits'][0]['_source']['title']

【讨论】:

  • 感谢您的回答,但如果我这样做了,它会返回“警告:不能将标量值用作数组”!
  • 使用 $search[$i]['hits']['total'] 不是数组,它是一个整数。
  • 我知道它是一个整数,但它返回给我那个错误
  • 由于 $search[$i]['hits']['total'] 是整数而不是数组,因此您不能使用 $search[$i]['hits']['total' ]['title'] 认为 $search[$i]['hits']['total'] 是一个数组。
  • 那么我怎样才能得到标题,你有什么建议吗:)
猜你喜欢
  • 2015-06-02
  • 2014-02-06
  • 2015-12-01
  • 2013-11-27
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多