PHP如何正确读取JSON记录并提取所有text字段值合并输出
问题原因
你的现有代码在遍历每一个text字段时,都会即时生成独立的JSON结构并输出,所以最终会得到多个分离的message对象,没有实现全局的文本拼接。
修复思路
- 遍历JSON前先初始化一个空字符串变量,用于累计所有提取到的
text内容 - 遍历过程中仅向该变量追加文本内容,不生成、输出JSON结构
- 所有内容遍历完成后,再将拼接好的完整文本放入
message字段,一次性输出JSON
修改后的完整代码
$spacing = " "; $output = ' { "language": "en", "textAngle": 0.0, "orientation": "Up", "regions": [{ "boundingBox": "236,480,851,2269", "lines": [{ "boundingBox": "646,480,441,115", "words": [{ "boundingBox": "646,480,441,115", "text": "Cont050" }] }, { "boundingBox": "308,576,198,106", "words": [{ "boundingBox": "308,576,198,106", "text": "Contoso" }] }, { "boundingBox": "301,683,386,137", "words": [{ "boundingBox": "301,683,80,75", "text": "123" }, { "boundingBox": "403,706,115,86", "text": "Main" }, { "boundingBox": "538,744,149,76", "text": "street" } ] }, { "boundingBox": "293,792,521,125", "words": [{ "boundingBox": "293,792,255,120", "text": "Redmond," }, { "boundingBox": "564,849,77,66", "text": "WA" }, { "boundingBox": "661,859,153,58", "text": "98052" } ] }, { "boundingBox": "278,1009,367,106", "words": [{ "boundingBox": "278,1009,367,106", "text": "987454-3210" }] }, { "boundingBox": "265,1231,257,92", "words": [{ "boundingBox": "265,1231,257,92", "text": "6/10/2019" }] }, { "boundingBox": "259,1352,596,92", "words": [{ "boundingBox": "259,1352,155,73", "text": "Sales" }, { "boundingBox": "434,1368,275,76", "text": "Associate:" }, { "boundingBox": "732,1380,123,63", "text": "Paul" } ] }, { "boundingBox": "249,1587,18,69", "words": [{ "boundingBox": "249,1587,18,69", "text": "1" }] }, { "boundingBox": "326,1591,327,92", "words": [{ "boundingBox": "326,1591,327,92", "text": "Cappuccino" }] }, { "boundingBox": "236,1842,498,75", "words": [{ "boundingBox": "236,1842,19,73", "text": "I" }, { "boundingBox": "315,1843,190,73", "text": "BACON" }, { "boundingBox": "529,1847,39,69", "text": "&" }, { "boundingBox": "593,1847,141,70", "text": "EGGS" } ] }, { "boundingBox": "350,1978,393,82", "words": [{ "boundingBox": "350,1978,393,82", "text": "Sunny-side-up" }] }, { "boundingBox": "445,2371,113,88", "words": [{ "boundingBox": "445,2371,113,88", "text": "Tax" }] }, { "boundingBox": "437,2644,166,105", "words": [{ "boundingBox": "437,2644,166,105", "text": "Total" }] } ] }, { "boundingBox": "1113,1580,153,465", "lines": [{ "boundingBox": "1113,1580,153,84", "words": [{ "boundingBox": "1113,1580,153,84", "text": "$220" }] }, { "boundingBox": "1139,1954,122,91", "words": [{ "boundingBox": "1139,1954,122,91", "text": "9.5" }] }] } ], "modelVersion": "2021-04-01" } '; $json = json_decode($output, true); // 初始化空字符串存储所有拼接的文本 $fullMessage = ''; foreach ($json['regions'] as $row1) { foreach ($row1['lines'] as $row2) { foreach ($row2['words'] as $row3) { // 追加当前text和分隔空格,去掉了原代码多余的点号 $fullMessage .= $row3['text'] . $spacing; } } } // 所有内容拼接完成后统一生成JSON输出 // 如果不需要末尾多余空格,可以替换为trim($fullMessage) $output_response = array( "message" => $fullMessage ); echo json_encode($output_response);
核心修改点
- 新增了全局拼接变量
$fullMessage,用于累计所有提取到的文本内容 - 删除了循环内部的JSON生成和输出逻辑,仅做文本追加
- 去掉了原代码中多余的点号,避免输出出现不必要的标点
- 所有遍历完成后统一生成JSON结构一次性输出
内容的提问来源于stack exchange,提问作者Nancy Moore
相关产品推荐
相关产品推荐

