On executing elastic search and storing the result in JSON form
stdClass Object (
[took] => 119
[timed_out] =>
[_shards] => stdClass Object (
[total] => 5
[successful] => 5
[failed] => 0
)
[hits] => stdClass Object (
[total] => 3
[max_score] => 1
[hits] => Array (
[0] => stdClass Object (
[_index] => movies
[_type] => movie
[_id] => 3
[_score] => 1
[_source] => stdClass Object (
[title] => The MATRIX
[year] => 1975
)
)
[1] => stdClass Object (
[_index] => movies
[_type] => movie
[_id] => 8
[_score] => 1
[_source] => stdClass Object (
[title] => The MATRIX
[year] => 1975
)
)
[2] => stdClass Object (
[_index] => movies
[_type] => movie
[_id] => 4
[_score] => 1
[_source] => stdClass Object (
[title] => The MATRIX
[year] => 1975
)
)
)
)
)
I want to get value of each movie and year in the above
I tried
foreach($result as $i)
{
echo $i->title;
echo $i->year;
}
Notice: Trying to get property of non-object in D:\xampp\htdocs\esearch\index.php on line 16
Notice: Trying to get property of non-object in D:\xampp\htdocs\esearch\index.php on line 17
How to get it?
hank
3,7681 gold badge26 silver badges37 bronze badges
asked Mar 27, 2014 at 7:05
Akash Kumar
6421 gold badge9 silver badges21 bronze badges
2 Answers 2
You can use following;
foreach($result->hits->hits as $movie)
{
echo $movie->_source->title;
echo $movie->_source->year;
}
answered Mar 27, 2014 at 7:11
Hüseyin BABAL
15.6k5 gold badges53 silver badges74 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Try this
foreach($result->hits->hits as $i)
{
echo $i->_source->title;
echo $i->_source->year;
}
answered Mar 27, 2014 at 7:14
sanjeev
4,6512 gold badges22 silver badges28 bronze badges
Comments
lang-php
json_decode($yourjson, true).$iisn't an object (seeing how it's119, then empty, and then an stdClass.