PHP从json获得巨大的多值数组值

时间:2016-04-29 18:32:48

标签: php arrays json

所以我刚开始使用JSON文件,我需要知道如何获取值" text"。基本上当它完成后我需要它说:

"如果我们完成了所有这些事情,我们就能够做到我们自己的生活方式并且#34;

我的JSON内容:

{
    "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."
            }]
        }]
    }]
}

也很抱歉没有非常具有描述性,但对于StackOverflow来说却是一个新手。

1 个答案:

答案 0 :(得分:1)

可能有更通用的答案,但此代码适合您的JSON。我希望到达存储text的正确级别。 JSON中的每个级别都是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;