It is required to extract the values ​​with the identifier "text" from all the JSON nodes of the string in order to assemble an expression (sentence) at the output.

Question: how to sort through all elements of this JSON string?

 { "language": "ru", "textAngle": 0.0, "orientation": "Up", "regions": [ { "boundingBox": "42,12,596,465", "lines": [ { "boundingBox": "42,12,557,49", "words": [ { "boundingBox": "42,13,64,41", "text": "ты" }, { "boundingBox": "120,13,175,48", "text": "БУДЕШЬ" }, { "boundingBox": "309,12,198,43", "text": "визжать" }, { "boundingBox": "521,13,78,41", "text": "как" } ] }, { "boundingBox": "180,71,286,49", "words": [ { "boundingBox": "180,71,286,49", "text": "МАНДРАГОРА," } ] }, { "boundingBox": "70,422,506,49", "words": [ { "boundingBox": "70,422,135,49", "text": "когда" }, { "boundingBox": "216,422,235,43", "text": "ПОЛУЧИШЬ" }, { "boundingBox": "465,423,111,41", "text": "мня" } ] }, { "boundingBox": "569,465,69,12", "words": [ { "boundingBox": "569,465,54,12", "text": "riSOVach." }, { "boundingBox": "625,469,13,7", "text": "ru" } ] } ] } ] } 
  • loop through first lines and then words. what is the problem? - Jean-Claude
  • one
    Use $words[] = array_walk_recursive(function ($value, $key){ if ($key == 'text'){ return $value; } }); echo implode(' ', $words); $words[] = array_walk_recursive(function ($value, $key){ if ($key == 'text'){ return $value; } }); echo implode(' ', $words); - Roman Kozin

1 answer 1

 $json_string = '{ "language":"ru", "textAngle":0.0, "orientation":"Up", "regions":[ { "boundingBox":"42,12,596,465", "lines":[ { "boundingBox":"42,12,557,49", "words":[ { "boundingBox":"42,13,64,41", "text":"ты" }, { "boundingBox":"120,13,175,48", "text":"БУДЕШЬ" }, { "boundingBox":"309,12,198,43", "text":"визжать" }, { "boundingBox":"521,13,78,41", "text":"как" } ] }, { "boundingBox":"180,71,286,49", "words":[ { "boundingBox":"180,71,286,49", "text":"МАНДРАГОРА," } ] }, { "boundingBox":"70,422,506,49", "words":[ { "boundingBox":"70,422,135,49", "text":"когда" }, { "boundingBox":"216,422,235,43", "text":"ПОЛУЧИШЬ" }, { "boundingBox":"465,423,111,41", "text":"мня" } ] }, { "boundingBox":"569,465,69,12", "words":[ { "boundingBox":"569,465,54,12", "text":"riSOVach." }, { "boundingBox":"625,469,13,7", "text":"ru" } ] } ] } ] }'; $obj=json_decode($json_string); 

Solution # 1 - not recommended, for warming up with a for cycle

  for ($n = 0; $n < count($obj->regions); $n++) { for ($j = 0; $j < count($obj->regions[$n]->lines); $j++) { for ($i = 0; $i < count($obj->regions[$n]->lines[$j]->words); $i++) { echo $obj->regions[$n]->lines[$j]->words[$i]->text." "; } } } 

Decision number 2 - recommended

 foreach($obj->regions as $regions) { foreach($regions->lines as $lines) { foreach($lines->words as $words) { echo $words->text . " "; } } } 
  • can for the general development of the participants are not so "toothy" (in a good way), the cons will be accompanied by specifics in the comments? - TimurVI
  • Because there is foreach and array_values - ilyaplot
  • @ilyaplot thanks for the comment, the controversy to inflate is not configured, but the decision is a working one. Not everyone can know the super duper is the perfect solution. - TimurVI
  • Perhaps not entirely working regions[0] process only the first key. - ilyaplot
  • corrected, thanks - TimurVI