如何在PHP中解析PDF2JSON文件

时间:2013-09-06 01:33:50

标签: php json parsing

使用PHP,如何从“text”标签中获取每个值?

这是我的json文件:

[{"number":1,"pages":33,"height":1188,"width":918,"fonts":[],
"text":[[108,108,23,21,2,"Some Text 1"],[108,131,6,21,2,"Some Text 2.."],[108,154,6,21,2,"Some Text 3.. "]]}]

到目前为止,这是我的PHP,

$data = json_decode(file_get_contents('file.json'));
$object = array();
foreach($data as $index=>$object) {
    foreach($object as $name=>$value) {
           //$output[$name][$index] = $value;

           echo $output[text][0];
           // .........
       echo $output[text][5];

    }
}

由于

2 个答案:

答案 0 :(得分:1)

请考虑以下示例:

$json = <<<JSON
[{"number":1,"pages":33,"height":1188,"width":918,"fonts":[], "text":[[108,108,23,21,2,"Some Text 1"],[108,131,6,21,2,"Some Text 2.."],[108,154,6,21,2,"Some Text 3.. "]]}]
JSON;

$data = json_decode($json, TRUE);

foreach($data[0]['text'] as $key => $array)
{
  var_dump($array[0], $array[5]);
}

输出

int 108

string 'Some Text 1' (length=11)

int 108

string 'Some Text 2..' (length=13)

int 108

string 'Some Text 3.. ' (length=14)

如果要遍历每个 text 结果,则必须使用至少两个循环:

foreach($data[0]['text'] as $key => $array)
  foreach($array as $text)
    echo $key, ' ', $text, PHP_EOL;

输出

0 108
0 108
0 23
0 21
0 2
0 Some Text 1

1 108
1 131
1 6
1 21
1 2
1 Some Text 2..

2 108
2 154
2 6
2 21
2 2
2 Some Text 3.. 

答案 1 :(得分:1)

另一种方法:

$json = '[{"number":1,"pages":33,"height":1188,"width":918,"fonts":[], "text":[[108,108,23,21,2,"Some Text 1"],[108,131,6,21,2,"Some Text 2.."],[108,154,6,21,2,"Some Text 3.. "]]}]';
$json_data = json_decode($json, TRUE);
array_walk(array_values($json_data[0]['text']), function($k) {
  print_r(array_values($k));
});