1

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
3
  • 1
    Try to convert your json to associative array with passing true on second parameter json_decode($yourjson, true). Commented Mar 27, 2014 at 7:11
  • The problem isn't the getter, it's the fact that $i isn't an object (seeing how it's 119, then empty, and then an stdClass. Commented Mar 27, 2014 at 7:12
  • @fian - already done that Commented Mar 27, 2014 at 7:19

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

 foreach($result->hits->hits as $i)
 {
 echo $i->_source->title;
 echo $i->_source->year;
 }
answered Mar 27, 2014 at 7:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.