0

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

这是我的 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];

    }
}

谢谢

4

2 回答 2

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));
});
于 2013-09-06T02:08:56.917 回答
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)

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

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.. 
于 2013-09-06T01:41:19.523 回答