Reputation: 2001
Here is my json,
{
"responseHeader":{
"status":0,
"QTime":15},
"response":{"numFound":0,"start":0,"docs":[]
},
"spellcheck":{
"suggestions":[
"abert",{
"numFound":10,
"startOffset":0,
"endOffset":5,
"origFreq":0,
"suggestion":[{
"word":"albert",
"freq":126},
{
"word":"aber t",
"freq":317},
{
"word":"alert",
"freq":58},
{
"word":"a bert",
"freq":13},
{
"word":"abort",
"freq":57},
{
"word":"a be rt",
"freq":1045},
{
"word":"avert",
"freq":37},
{
"word":"ab e rt",
"freq":335},
{
"word":"aberr",
"freq":21},
{
"word":"ab er t",
"freq":317}]},
"enstin",{
"numFound":10,
"startOffset":6,
"endOffset":12,
"origFreq":0,
"suggestion":[{
"word":"einstein",
"freq":92},
{
"word":"ens tin",
"freq":44},
{
"word":"enshrin",
"freq":13},
{
"word":"en s tin",
"freq":673},
{
"word":"enjoin",
"freq":12},
{
"word":"e ns tin",
"freq":335},
{
"word":"entwin",
"freq":8},
{
"word":"ens t in",
"freq":317},
{
"word":"epstein",
"freq":8},
{
"word":"en st in",
"freq":231}]},
"correctlySpelled",false,
"collation",[
"collationQuery","albert einstein",
"hits",154,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","einstein"]],
"collation",[
"collationQuery","(aber t) einstein",
"hits",375,
"misspellingsAndCorrections",[
"abert","aber t",
"enstin","einstein"]],
"collation",[
"collationQuery","albert (ens tin)",
"hits",335,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","ens tin"]],
"collation",[
"collationQuery","albert enshrin",
"hits",137,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","enshrin"]],
"collation",[
"collationQuery","alert einstein",
"hits",139,
"misspellingsAndCorrections",[
"abert","alert",
"enstin","einstein"]]]}}
I am trying to get the following field
"collation",[
"collationQuery","albert einstein",
"hits",154,
"misspellingsAndCorrections",[
"abert","albert",
"enstin","einstein"]],
specifically 'albert einstein'. I tried the below code, but getting an error.
$myArray = json_decode($response, true);
foreach ($myArray['collation'] as $doc) {
echo $doc[0];
}
Upvotes: 0
Views: 90
Reputation: 179994
Lots of problems here.
responseHeader
, response
, spellcheck
are the root-level fields. Everything else is nested under them. Do print_r($myArray)
for what json_decode
is parsing out.$myArray
is an object, not an array. Objects are accessed like $myArray->response
, not $myArray['response']
.What you're calling "a field" is not a field. json_encode
sees the structure of that data as:
[6] => collation
[7] => Array
(
[0] => collationQuery
[1] => albert einstein
[2] => hits
[3] => 154
[4] => misspellingsAndCorrections
[5] => Array
(
[0] => abert
[1] => albert
[2] => enstin
[3] => einstein
)
)
If this is your code outputting the JSON, you're going to need to structure it much better. If it's someone else's code, they're mean.
Upvotes: 2