-1

So I just started working with JSON files and I need to know how to get the values "text". Basically when its done running through I need it to say:

"IF WE DID ALL THE THINGS WE ARE CAPABLE OF DOING WE WOULD LITERALLY ASTOUND OURSELVES"

My JSON contents:

{
    "language": "en",
    "textAngle": 0,
    "orientation": "Up",
    "regions": [{
        "boundingBox": "791,118,592,838",
        "lines": [{
            "boundingBox": "799,118,453,58",
            "words": [{
                "boundingBox": "799,118,102,58",
                "text": "IF"
            }, {
                "boundingBox": "937,119,109,57",
                "text": "WE"
            }, {
                "boundingBox": "1087,118,165,58",
                "text": "DID"
            }]
        }, {
            "boundingBox": "792,184,512,176",
            "words": [{
                "boundingBox": "792,184,512,176",
                "text": "ALL"
            }]
        }, {
            "boundingBox": "796,368,515,60",
            "words": [{
                "boundingBox": "796,368,155,59",
                "text": "THE"
            }, {
                "boundingBox": "993,368,318,60",
                "text": "THINGS"
            }]
        }, {
            "boundingBox": "791,441,312,57",
            "words": [{
                "boundingBox": "791,441,108,57",
                "text": "WE"
            }, {
                "boundingBox": "937,441,166,57",
                "text": "ARE"
            }]
        }, {
            "boundingBox": "797,508,586,87",
            "words": [{
                "boundingBox": "797,508,586,87",
                "text": "CAPABLE"
            }]
        }, {
            "boundingBox": "795,607,452,72",
            "words": [{
                "boundingBox": "795,607,106,59",
                "text": "OF"
            }, {
                "boundingBox": "941,607,306,72",
                "text": "DOING,"
            }]
        }, {
            "boundingBox": "791,678,424,60",
            "words": [{
                "boundingBox": "791,680,108,57",
                "text": "WE"
            }, {
                "boundingBox": "937,678,278,60",
                "text": "WOULD"
            }]
        }, {
            "boundingBox": "793,750,498,59",
            "words": [{
                "boundingBox": "793,750,498,59",
                "text": "LITERALLY"
            }]
        }, {
            "boundingBox": "791,821,390,60",
            "words": [{
                "boundingBox": "791,821,390,60",
                "text": "ASTOUND"
            }]
        }, {
            "boundingBox": "795,893,531,63",
            "words": [{
                "boundingBox": "795,893,531,63",
                "text": "OURSELVES."
            }]
        }]
    }]
}

also sorry for not being very descriptive but im very new to StackOverflow.

Rajdeep Paul
  • 16,739
  • 3
  • 17
  • 37
Techwizmatt
  • 63
  • 1
  • 1
  • 6
  • 1
    did you try something? Show us some code, we can help you to improve/fix it. But we'll not write the code for you. – olibiaz Apr 29 '16 at 18:46
  • check this answer http://stackoverflow.com/a/18362216/6127393 – Arleigh Hix Apr 29 '16 at 19:10
  • I agree with @olibiaz: show what you've tried, because this is just a trivial traversal and I cannot understand what could possible go wrong. – Alexander Guz Apr 29 '16 at 20:21
  • Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Apr 30 '16 at 05:03

1 Answers1

1

There may be a more generic answer, but this code fits your JSON. I am looking to get to the right level where the text is stored. Each level in the JSON is another level of foreach.

 $json = '{ "language": "en" ... ';

 $decoded = json_decode($json, true);

 $result = "";

 foreach($decoded['regions'] as $region) {
   foreach($region['lines'] as $lines) {
     foreach($lines["words"] as $words) {
       $result .= $words["text"] . " ";
     }       
   }
 }

 echo $result;
Tina Vall
  • 172
  • 3
  • 11